인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

seongmooklim2010's profile image
seongmooklim2010

asked

Algorithm Course for Smart Programming

Application of Recursion - Maze Finding 1

Python 으로 구현해보았습니다.

Written on

·

204

1

class Maze:
    def findMazePath(self, x, y):
        if (x < 0 or y < 0 | x >= N or y >= N): #지도 범위를 넘어선곳
            return False

        elif (maze[x][y] != PATHWAY_COLOR): # White 0 #벽
            return False

        elif (x == N - 1 & y == N - 1): # 최종 목적지
            maze[x][y] = PATH_COLOR #Green 3
            return True

        else:
            maze[x][y] = PATH_COLOR #Green 3 # 계속 갈 수 있는 길인지, 막힌 길인지 파악이 안된 길. 일단 가보는 길
            if (self.findMazePath(x - 1, y) | self.findMazePath(x, y + 1) | \
                self.findMazePath(x + 1, y) | self.findMazePath(x, y - 1)):
                return True
            maze[x][y] = BLOCKED_COLOR #Red 2 $ 위의 if 구문에서 Return 받아서 가면 안되는 길 
            return False

if __name__ == '__main__':
    N = 8
    maze = [[0, 0, 0, 0, 0, 0, 0, 1],
            [0, 1, 1, 0, 1, 1, 0, 1],
            [0, 0, 0, 0, 0, 0, 0, 1],
            [0, 1, 0, 0, 1, 1, 0, 0],
            [0, 1, 1, 1, 0, 0, 1, 1],
            [0, 1, 0, 0, 0, 1, 0, 1],
            [0, 0, 0, 1, 0, 0, 0, 1],
            [0, 1, 1, 1, 0, 1, 0, 0]]

    PATHWAY_COLOR = 0 # 원래 길
    WALL_COLOR = 1 # 벽
    BLOCKED_COLOR = 2 # 이 길로 계속 가면 가다 도중에 막히는 길
    PATH_COLOR = 3 # 이 길로 계속 가면 끝까지 갈 수 있는 길

    s = Maze()
    print(maze)
    s.findMazePath(0, 0)
    print(maze)
algorithm

Answer

This question is waiting for answers
Be the first to answer!
seongmooklim2010's profile image
seongmooklim2010

asked

Ask a question