-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
96 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 문제: 게임맵 | ||
https://school.programmers.co.kr/learn/courses/30/lessons/1844 | ||
|
||
## 아이디어 | ||
- 가장 빠른 경로를 찾는 것이기 때문에 BFS로 풀어야 함 | ||
|
||
## 풀이 | ||
1. answer, visited, directions 초기화 | ||
2. goal, queue에 끝점, 시작점 넣고 visited에도 추가 | ||
- queue에는 x 좌표, y 좌표, answer 튜플로 넣기 | ||
3. queue에 원소가 존재하는 동안 while문 순회 | ||
4. queue에서 popleft한 위치가 goal이면 answer 리턴 | ||
5. directions에 따라 순회하면서 새로운 위치가 map 안에 있고, 장애물이 없으며, visited에 없으면 | ||
- answer += 1 | ||
- visited에 추가 | ||
- queue에 추가 | ||
6. while문 안에서 리턴되지 못했으면 goal에 도착하지 못한 것이므로 -1 리턴 | ||
|
||
```python | ||
from collections import deque | ||
|
||
# 빠른 루트를 찾아야 하니 bfs | ||
|
||
def solution1(maps): | ||
n, m = len(maps), len(maps[0]) | ||
directions = [(1,0), (-1,0), (0,1), (0,-1)] | ||
goal = (n-1, m-1) | ||
answer = 0 | ||
visited = set() | ||
q = deque((0,0,answer)) | ||
visited.add((0,0)) | ||
|
||
while q: | ||
curr_x, curr_y, answer = q.popleft() | ||
if (curr_x, curr_y) == goal: | ||
return answer | ||
else: | ||
for d in directions: | ||
curr_x, curr_y = curr_x + d[0], curr_y + d[1] | ||
if 0<curr_x<=len(maps) and 0 < curr_y < len(maps[0]) and maps[curr_x][curr_y] == 1 and (curr_x, curr_y) not in visited: | ||
answer += 1 | ||
visited.add((curr_x, curr_y)) | ||
q.append((curr_x, curr_y, answer)) | ||
return -1 | ||
|
||
``` | ||
|
||
## 배운 점 | ||
- bfs, dfs의 대표적인 문제 구조가 이제 보이는 듯함 | ||
- visited, answer, directions 초기화 후 bfs면 queue, dfs면 stack으로 풀기 | ||
- bfs에서 while문에서 goal에 도착하면 return하는 식으로 답을 찾으면 while문 안에서 리턴하고 답이 없으면 while문 밖에서 -1 리턴 | ||
- DFS로 풂면 효율성 테스트에서 실패함 | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 문제: 게임맵 | ||
https://school.programmers.co.kr/learn/courses/30/lessons/1844 | ||
|
||
## 아이디어 | ||
- 가장 빠른 경로를 찾는 것이기 때문에 BFS로 풀어야 함 | ||
|
||
## 풀이 | ||
1. answer, visited, directions 초기화 | ||
2. goal, queue에 끝점, 시작점 넣고 visited에도 추가 | ||
- queue에는 x 좌표, y 좌표, answer 튜플로 넣기 | ||
3. queue에 원소가 존재하는 동안 while문 순회 | ||
4. queue에서 popleft한 위치가 goal이면 answer 리턴 | ||
5. directions에 따라 순회하면서 새로운 위치가 map 안에 있고, 장애물이 없으며, visited에 없으면 | ||
- answer += 1 | ||
- visited에 추가 | ||
- queue에 추가 | ||
6. while문 안에서 리턴되지 못했으면 goal에 도착하지 못한 것이므로 -1 리턴 | ||
|
||
```python | ||
from collections import deque | ||
|
||
# 빠른 루트를 찾아야 하니 bfs | ||
|
||
def solution1(maps): | ||
n, m = len(maps), len(maps[0]) | ||
directions = [(1,0), (-1,0), (0,1), (0,-1)] | ||
goal = (n-1, m-1) | ||
answer = 0 | ||
visited = set() | ||
q = deque((0,0,answer)) | ||
visited.add((0,0)) | ||
|
||
while q: | ||
curr_x, curr_y, answer = q.popleft() | ||
if (curr_x, curr_y) == goal: | ||
return answer | ||
else: | ||
for d in directions: | ||
curr_x, curr_y = curr_x + d[0], curr_y + d[1] | ||
if 0<curr_x<=len(maps) and 0 < curr_y < len(maps[0]) and maps[curr_x][curr_y] == 1 and (curr_x, curr_y) not in visited: | ||
answer += 1 | ||
visited.add((curr_x, curr_y)) | ||
q.append((curr_x, curr_y, answer)) | ||
return -1 | ||
|
||
``` | ||
|
||
## 배운 점 | ||
- bfs, dfs의 대표적인 문제 구조가 이제 보이는 듯함 | ||
- visited, answer, directions 초기화 후 bfs면 queue, dfs면 stack으로 풀기 | ||
- bfs에서 while문에서 goal에 도착하면 return하는 식으로 답을 찾으면 while문 안에서 리턴하고 답이 없으면 while문 밖에서 -1 리턴 | ||
- DFS로 풂면 효율성 테스트에서 실패함 | ||
|