-
Notifications
You must be signed in to change notification settings - Fork 0
/
4179.cpp
89 lines (67 loc) · 1.66 KB
/
4179.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
83
84
85
86
87
88
89
#include<iostream>
#include<queue>
using namespace std;
string board[1000];
int visit1[1000][1000];
int visit2[1000][1000];
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int col, row;
cin >> col >> row;
for (int i = 0; i < col; i++) {
fill(visit1[i], visit1[i] + row, -1);
fill(visit2[i], visit2[i] + row, -1);
}
for (int i = 0; i < col; i++) {
cin >> board[i];
}
queue<pair<int, int>>q1;
queue<pair<int, int>>q2;
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
if (board[i][j] == 'F') {
q1.push({ i,j });
visit1[i][j] = 0;
}
else if (board[i][j] == 'J') {
q2.push({ i,j });
visit2[i][j] = 0;
}
}
}
while (!q1.empty()) {
pair<int, int> cur = q1.front(); q1.pop();
for (int i = 0; i < 4; i++) {
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if (nx < 0 || nx >= col || ny < 0 || ny >= row) {
continue;
}
if (board[nx][ny] != '.' || visit1[nx][ny] > -1)
continue;
q1.push({ nx,ny });
visit1[nx][ny] = visit1[cur.first][cur.second] + 1;
}
}
while (!q2.empty()) {
pair<int, int> cur = q2.front(); q2.pop();
for (int i = 0; i < 4; i++) {
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if (nx < 0 || nx >= col || ny < 0 || ny >= row) {
cout << visit2[cur.first][cur.second] + 1 << endl;
return 0;
}
if(board[nx][ny] != '.' || visit2[nx][ny] > -1)
continue;
if (visit1[nx][ny] != -1 && visit1[nx][ny] <= visit2[cur.first][cur.second]+1)
continue;
q2.push({ nx,ny });
visit2[nx][ny] = visit2[cur.first][cur.second] + 1;
}
}
cout << "IMPOSSIBLE " << endl;
}