-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2206.cpp
82 lines (62 loc) · 1.43 KB
/
2206.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Created by 융미 on 2019-04-20.
//2206 벽 부수고 이동하기
#include <iostream>
#include <queue>
#include <tuple>
#include <string>
#include <cstring>
using namespace std;
#define MAX 1000
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
int n, m;
string arr[MAX];
int dis[MAX][MAX][2];
void getInput();
int bfs();
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
getInput();
cout << bfs();
return 0;
}
void getInput(){
cin >> n >> m;
string s;
for(int i = 0; i<n; i++)
{
cin >> s;
arr[i] = s;
}
}
int bfs()
{
memset(dis, -1, sizeof(dis));
queue<tuple<int,int,int>> q;
q.push(make_tuple(0,0,0));
dis[0][0][0] = 1;
while(!q.empty())
{
auto cur = q.front();
q.pop();
int x = get<0>(cur);
int y = get<1>(cur);
int check = get<2>(cur);
if(x == n-1 && y == m-1) return dis[n-1][m-1][check];
for(int i = 0; i<4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
int tocheck = check;
if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if(dis[nx][ny][check] != -1) continue;
if(arr[nx][ny] == '1' && tocheck == 1 ) continue;
if(arr[nx][ny] == '1' && tocheck == 0) tocheck = 1;
dis[nx][ny][tocheck] = dis[x][y][check] + 1;
q.push(make_tuple(nx,ny,tocheck));
}
}
return -1;
}