반응형
문제
https://www.acmicpc.net/problem/2178
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net
풀이
bfs 탐색을 (1,1)에서 시작해서 (N,M)에 도착했을 때 지나가는 level의 수가 최소 횟수가 된다.
result = (1,1)에서 (N,M)까지 이동할 때 지나야하는 최소 칸 수 라고 할 때,
bfs 탐색을 진행하면서 현재 level에 해당되는 좌표들을 모두 방문한 후에 result 값을 증가시켜주면 된다.
현재 level에 해당하는 좌표를 모두 방문하려면, bfs 탐색에서 현재 큐에 저장되어 있는 데이터 수 만큼 큐에서 꺼내서 다음 방문할 좌표를 찾는 작업을 하면 된다.
코드
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#include <iostream>
#include <queue>
using namespace std;
int N, M;
char graph[102][102];
bool visit[102][102];
int result;
pair<int, int> direction[4] = {{1,0}, {-1,0}, {0,1}, {0,-1}};
void bfs()
{
queue<pair<int, int>> q;
q.push({1, 1});
visit[1][1] = true;
while(!q.empty())
{
int len = q.size();
while(len--)
{
int i=q.front().first, j=q.front().second; q.pop();
if (i == N && j == M)
{
result++;
return;
}
for (int l=0; l<4; l++)
{
int nextI = i + direction[l].first;
int nextJ = j + direction[l].second;
if (graph[nextI][nextJ]=='1' && !visit[nextI][nextJ])
{
q.push({nextI, nextJ});
visit[nextI][nextJ] = true;
}
}
}
result++;
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(0);
cin >> N >> M;
for (int i=1; i<=N; i++)
for (int j=1; j<=M; j++) cin >> graph[i][j];
bfs();
cout << result;
return 0;
}
|
cs |
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
from collections import deque
def bfs(x, y):
cnt = 0
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
q = deque([[0, 0]])
maze[y][x] = 0
while q:
level = len(q)
cnt += 1
for _ in range(level):
cur_y, cur_x = q.popleft()
if cur_y == n - 1 and cur_x == m - 1:
return cnt
for i in range(4):
next_y = cur_y + dy[i]
next_x = cur_x + dx[i]
if 0 <= next_y < n and 0 <= next_x < m and maze[next_y][next_x] == 1:
q.append([next_y, next_x])
maze[next_y][next_x] = 0
n, m = map(int, input().split())
maze = [list(map(int, input())) for _ in range(n)]
print(bfs(0, 0))
|
cs |
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#include <iostream>
#include <queue>
using namespace std;
int N, M;
char graph[102][102];
bool visit[102][102];
int result;
pair<int, int> direction[4] = {{1,0}, {-1,0}, {0,1}, {0,-1}};
void bfs()
{
queue< pair<int, int> > q;
q.push({1, 1});
visit[1][1] = true;
while(!q.empty())
{
int len = q.size();
while(len--)
{
int i=q.front().first, j=q.front().second; q.pop();
if (i == N && j == M)
{
result++;
return;
}
for (int l=0; l<4; l++)
{
int nextI = i + direction[l].first;
int nextJ = j + direction[l].second;
if (graph[nextI][nextJ]=='1' && !visit[nextI][nextJ])
{
q.push({nextI, nextJ});
visit[nextI][nextJ] = true;
}
}
}
result++;
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(0);
cin >> N >> M;
for (int i=1; i<=N; i++)
for (int j=1; j<=M; j++) cin >> graph[i][j];
bfs();
cout << result;
return 0;
}
|
cs |
반응형
'BOJ' 카테고리의 다른 글
BOJ 15649 - N과 M (1) (0) | 2024.09.12 |
---|---|
BOJ 15651 - N과 M (3) (0) | 2024.09.11 |
BOJ 1018 - 체스판 다시 칠하기 (0) | 2024.08.24 |
BOJ 10872 - 팩토리얼 (0) | 2024.08.14 |
BOJ 1715 - 카드 정렬하기 (0) | 2024.08.06 |