Skip to content

Commit

Permalink
add p_1844_게임맵.md
Browse files Browse the repository at this point in the history
  • Loading branch information
terri1102 authored Aug 25, 2024
1 parent 47e62d1 commit e13fdaa
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 96 deletions.
96 changes: 0 additions & 96 deletions Algorithms/GraphAlgorithms/BFS_and_DFS/dfs_bfs_게임맵.py

This file was deleted.

53 changes: 53 additions & 0 deletions Algorithms/GraphAlgorithms/BFS_and_DFS/p_1844_게임맵.md
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로 풂면 효율성 테스트에서 실패함

53 changes: 53 additions & 0 deletions StudyGroups/TIL/2024/AUG/2024_08_25.md
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로 풂면 효율성 테스트에서 실패함

0 comments on commit e13fdaa

Please sign in to comment.