-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2178.cpp
More file actions
68 lines (59 loc) · 1.46 KB
/
2178.cpp
File metadata and controls
68 lines (59 loc) · 1.46 KB
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
63
64
65
66
67
68
#include <bits/stdc++.h>
#define MAX 100001
#define SETTING ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std ;
int N, M, cnt = 1 ;
bool graph[101][101] ;
bool arr[101][101] ;
int plus_x[] = {1, 0, -1, 0} ;
int plus_y[] = {0, 1, 0, -1} ;
void BFS()
{
queue< pair<int, int> > que ;
que.push(make_pair(1,1)) ;
while(!que.empty())
{
int size = que.size() ;
for(int i = 0 ; i < size ; i++)
{
pair<int, int> curr = que.front() ;
que.pop() ;
if(curr.first == N && curr.second == M) return ;
for(int j = 0 ; j < 4 ; j++)
{
int next_x = curr.first + plus_x[j] ;
int next_y = curr.second + plus_y[j] ;
if(next_x < 1 || next_y < 1 || next_x > N || next_y > M) continue ;
if(arr[next_x][next_y]) continue ;
if(graph[next_x][next_y])
{
que.push(make_pair(next_x, next_y)) ;
arr[next_x][next_y] = 1 ;
}
}
arr[curr.first][curr.second] = 1 ;
}
cnt++ ;
}
}
void InputSetting()
{
string tmp ;
cin >> N >> M ;
for(int i = 1 ; i <= N ; i++)
{
cin >> tmp ;
for(int j = 1 ; j <= M ; j++)
{
graph[i][j] = tmp[j - 1] - '0' ;
}
}
arr[1][1] = 1 ;
}
int main()
{
SETTING ;
InputSetting() ;
BFS() ;
cout << cnt ;
}