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
|
#include <iostream>
#include <vector>
#include<queue>
#include<algorithm>
using namespace std;
vector<int> g[200001] = {};
bool visit[200001] = {};
int n,m,k;
int result[200001] = {};
int abc = 0;
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
void bfs(int x,int y)
{
queue<pair<int,int>> a;
a.push({ x,y });
while (!a.empty())
{
int xx = a.front().first;
int yy = a.front().second;
a.pop();
for (int i = 0; i <4; i++)
{
int x1 = xx + dx[i];
int y1 = yy + dy[i];
if (x1 <= 0 || x1 > n || y1 < 0 || y1 >= m)
continue;
if(!g[x1][y1])
continue;
if(g[x1][y1]== 1)
{
g[x1][y1] = g[xx][yy]+1;
a.push({ x1,y1 });
}
}
}
}
int main(void)
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
string s;
cin >> s;
for (int j = 0; j < m; j++)
{
g[i].push_back(s[j] - '0');
}
}
bfs(1, 0);
cout << g[n][m-1];
return 0;
}
|
cs |
https://www.acmicpc.net/problem/2178
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net
'백준' 카테고리의 다른 글
C++ 섬의 개수 (0) | 2023.02.17 |
---|---|
C++ 1012 유기농 배추 (0) | 2023.02.16 |
C++ 24445 알고리즘 수업 - 너비 우선 탐색 2 (0) | 2023.02.16 |
C++ 24444 알고리즘 수업 - 너비 우선 탐색 1 (0) | 2023.02.16 |
C++ 24480 알고리즘 수업 - 깊이 우선 탐색 2 (0) | 2023.02.16 |