-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCastel2.cpp
More file actions
84 lines (77 loc) · 2.01 KB
/
Copy pathCastel2.cpp
File metadata and controls
84 lines (77 loc) · 2.01 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <fstream>
#include <queue>
using namespace std;
ifstream cin("castel2.in");
ofstream cout("castel2.out");
int const di[] = {-1, 0, 1, 0};
int const dj[] = {0, 1, 0, -1};
int const NMAX = 1005;
int n, m, k, d[NMAX][NMAX];
char mat[NMAX][NMAX];
inline bool ok(int i, int j) {
if (i < 1 || j < 1 || i > n || j > m)
return false;
return true;
}
int lee(int istart, int jstart) {
if (mat[istart][jstart] != '-')
return -1;
queue < pair < int, int > > q;
q.push({istart, jstart});
d[istart][jstart] = 1;
mat[istart][jstart] = '*';
while (!q.empty()) {
int r = q.front().first;
int c = q.front().second;
q.pop();
if (r == n && c == m)
return d[r][c];
for (int i = 0; i < 4; i++) {
int rr = r + di[i];
int cc = c + dj[i];
if (ok(rr, cc) && mat[rr][cc] == '-') {
q.push({rr, cc});
d[rr][cc] = d[r][c] + 1;
mat[rr][cc] = '*';
}
}
}
return -1;
}
int main() {
cin >> n >> m >> k;
queue < pair < int, int > > q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> mat[i][j];
if (mat[i][j] == 'Z') {
q.push({i, j});
d[i][j] = 0;
}
}
while (!q.empty()) {
int r = q.front().first;
int c = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int rr = r + di[i];
int cc = c + dj[i];
if (ok(rr, cc) && mat[rr][cc] == '-') {
d[rr][cc] = d[r][c] + 1;
if (d[rr][cc] > k)
continue;
q.push({rr, cc});
mat[rr][cc] = '*';
}
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= m; j++)
// cout << mat[i][j] << ' ';
// cout << '\n';
// }
cout << lee(1, 1) << '\n';
cin.close();
cout.close();
return 0;
}