Created
May 23, 2017 23:26
-
-
Save sat0b/876381a0ec4dda084fa4ed1a63b31651 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 迷路の経路を出力 | |
| import copy | |
| N = 10 | |
| M = 10 | |
| meiro = """\ | |
| #S######.# | |
| ......#..# | |
| .#.##.##.# | |
| .#........ | |
| ##.##.#### | |
| ....#....# | |
| .#######.# | |
| ....#..... | |
| .####.###. | |
| ....#...G# | |
| """ | |
| print("Map") | |
| print(meiro) | |
| meiro = [list(m) for m in meiro.split('\n')] | |
| meiro_org = copy.deepcopy(meiro) | |
| def bfs(): | |
| queue = [] | |
| start = (0, 1) | |
| goal = (9, 8) | |
| queue.append([start]) | |
| while True: | |
| path = queue.pop(0) | |
| x, y = path[-1] | |
| if (x, y) == goal: | |
| return path | |
| for nx, ny in ((x-1, y), (x+1, y), (x, y-1), (x, y+1)): | |
| if 0 <= nx < N and 0 <= ny < M and meiro[nx][ny] != '#': | |
| meiro[nx][ny] = '#' | |
| newpath = path.copy() | |
| newpath.append((nx, ny)) | |
| queue.append(newpath) | |
| path = bfs() | |
| for x, y in path: | |
| meiro_org[x][y] = '*' | |
| print("Result") | |
| print("\n".join(["".join(line) for line in meiro_org])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Map | |
| #S######.# | |
| ......#..# | |
| .#.##.##.# | |
| .#........ | |
| ##.##.#### | |
| ....#....# | |
| .#######.# | |
| ....#..... | |
| .####.###. | |
| ....#...G# | |
| Result | |
| #*######.# | |
| .**...#..# | |
| .#*##.##.# | |
| .#****.... | |
| ##.##*#### | |
| ....#****# | |
| .#######*# | |
| ....#****. | |
| .####*###. | |
| ....#****# | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment