/*
* Author: bubu2006
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int H, W;
cin >> H >> W;
vector<string> m(H);
for(int i = 0; i < H; i++) cin >> m[i];
vector<int> dy = {0, 0, -1, 1};
vector<int> dx = {1, -1, 0, 0};
const int INF = 1e8;
vector<vector<int>> d(H, vector<int>(W, INF));
deque<pair<int, int>> q;
q.push_back({0, 0});
d[0][0] = 1;
int ans = 1;
while(!q.empty()) {
pair<int, int> cur = q.front();
int y = cur.first;
int x = cur.second;
ans = max(ans, d[y][x]);
q.pop_front();
for(int k = 0; k < 4; k++) {
int ny = y + dy[k];
int nx = x + dx[k];
if(ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
if(m[ny][nx] == '.') continue;
int w = (m[y][x] != m[ny][nx]);
if(d[ny][nx] > d[y][x] + w) {
d[ny][nx] = d[y][x] + w;
if(w) q.push_back({ny, nx});
else q.push_front({ny, nx});
}
}
}
cout << ans << '\n';
}
// ........ RRR..... FFR.....
// ........ ..RRR... .FRRR...
// ........ ..R..... .FFFFF..
// ........ ..RRRR.R ..RRRFFR
// ........ .....RRR .....FFF
// i had a greedy that always would work but the 0/1 bfs is just too clean
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1740 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
6 ms |
1484 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
4 ms |
844 KB |
Output is correct |
13 |
Correct |
2 ms |
716 KB |
Output is correct |
14 |
Correct |
2 ms |
840 KB |
Output is correct |
15 |
Correct |
12 ms |
1860 KB |
Output is correct |
16 |
Correct |
11 ms |
1740 KB |
Output is correct |
17 |
Correct |
7 ms |
1740 KB |
Output is correct |
18 |
Correct |
8 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
716 KB |
Output is correct |
2 |
Correct |
36 ms |
8524 KB |
Output is correct |
3 |
Correct |
195 ms |
81640 KB |
Output is correct |
4 |
Correct |
60 ms |
19444 KB |
Output is correct |
5 |
Correct |
142 ms |
46216 KB |
Output is correct |
6 |
Correct |
708 ms |
95348 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
41 ms |
8524 KB |
Output is correct |
14 |
Correct |
21 ms |
5256 KB |
Output is correct |
15 |
Correct |
15 ms |
5724 KB |
Output is correct |
16 |
Correct |
19 ms |
3936 KB |
Output is correct |
17 |
Correct |
100 ms |
20900 KB |
Output is correct |
18 |
Correct |
60 ms |
20708 KB |
Output is correct |
19 |
Correct |
54 ms |
19336 KB |
Output is correct |
20 |
Correct |
54 ms |
17908 KB |
Output is correct |
21 |
Correct |
115 ms |
47756 KB |
Output is correct |
22 |
Correct |
159 ms |
46152 KB |
Output is correct |
23 |
Correct |
195 ms |
39860 KB |
Output is correct |
24 |
Correct |
113 ms |
46620 KB |
Output is correct |
25 |
Correct |
359 ms |
81596 KB |
Output is correct |
26 |
Correct |
760 ms |
112816 KB |
Output is correct |
27 |
Correct |
665 ms |
103268 KB |
Output is correct |
28 |
Correct |
731 ms |
95408 KB |
Output is correct |
29 |
Correct |
705 ms |
92520 KB |
Output is correct |
30 |
Correct |
737 ms |
96324 KB |
Output is correct |
31 |
Correct |
538 ms |
52888 KB |
Output is correct |
32 |
Correct |
674 ms |
97940 KB |
Output is correct |