깊이 우선 탐색

박진은·2022년 5월 5일
0

자료구조

목록 보기
7/37

미로탐색

class Stack:
def **init**(self):
self.top = []
def isEmpty(self):
    return len(self.top) == 0

def push(self, item):
    self.top.append(item)

def pop(self):
    if len(self.top) != 0:
        return self.top.pop(-1)  # -1 대신에 length of top 에 -1도 가능합니다.

def peek(self):
    if len(self.top) != 0:
        return self.top[-1]

def display(self):
    print(self.top)

def __sizeof__(self):
    return len(self.pop)

def __str__(self):
    return str(self.top)
map = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 'x'],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
maplen = len(map)
def findroad(x, y):
if x < 0 or x > maplen - 1 or y < 0 or y > maplen - 1:
return False
elif map[x][y] == 0 or map[x][y] == 'x':
return True
def DPSE():
    stack = Stack()

    stack.push((0, 1))

    while not stack.isEmpty():
        here = stack.pop()
        print("현재위치: ", here, end=" ")
        (x, y) = here

        if map[x][y] == 'x':
            return True
        else:
            map[x][y] = '.'
            if findroad(x + 1, y): stack.push((x + 1, y))
            if findroad(x - 1, y): stack.push((x - 1, y))
            if findroad(x, y - 1): stack.push((x, y - 1))
            if findroad(x, y + 1): stack.push((x, y + 1))
        print("stack: ", stack)
    return False
result = DPSE()
print("탐색결과: ", result)

진은아 그냥 외워 어차피 너 이거 생각 못해

for i in range(len(string)-1,0,-1): 진은아 문자열 반대로 출력할때 이렇게 안하면 ㅈ된다 문자열 총 길이에서 -1 해줘야 하는거야

range() 사용하면 범위에서 제한되는 거여, range(a,b,c) 이렇게 하면 a 부터 b-1까지 c 간격으로 조지는거다

profile
코딩

0개의 댓글