#include <bits/stdc++.h>
using namespace std;
vector<int> dx{-1, 1, 0, 0}, dy{0, 0, -1, 1};
int main() {
int h, w;
cin >> h >> w;
vector<string> field(h);
vector<vector<int>> rank(h, vector<int>(w, 0));
for (int i = 0; i < h; ++i) cin >> field[i];
int m = 1;
rank[0][0] = 1;
deque<pair<int, int>> q;
q.push_back({0, 0});
while (!q.empty()) {
pair<int, int> s = q.front();
m = max(m, rank[s.first][s.second]);
q.pop_front();
for (int i = 0; i < 4; ++i) {
int x = s.first + dx[i], y = s.second + dy[i];
if (x < 0 || y < 0 || x >= h || y >= w || rank[x][y] > 0 || field[x][y] == '.') continue;
int r = rank[s.first][s.second];
if (field[s.first][s.second] == field[x][y]) {
q.push_front({x, y});
rank[x][y] = r;
} else {
q.push_back({x, y});
rank[x][y] = r + 1;
}
}
}
cout << m;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
1740 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
9 ms |
1360 KB |
Output is correct |
5 |
Correct |
4 ms |
716 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
1 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 |
3 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
6 ms |
716 KB |
Output is correct |
13 |
Correct |
4 ms |
716 KB |
Output is correct |
14 |
Correct |
4 ms |
716 KB |
Output is correct |
15 |
Correct |
15 ms |
1812 KB |
Output is correct |
16 |
Correct |
18 ms |
1740 KB |
Output is correct |
17 |
Correct |
13 ms |
1724 KB |
Output is correct |
18 |
Correct |
10 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
716 KB |
Output is correct |
2 |
Correct |
74 ms |
8792 KB |
Output is correct |
3 |
Correct |
541 ms |
93352 KB |
Output is correct |
4 |
Correct |
140 ms |
22108 KB |
Output is correct |
5 |
Correct |
360 ms |
47036 KB |
Output is correct |
6 |
Correct |
938 ms |
106744 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
73 ms |
8856 KB |
Output is correct |
14 |
Correct |
41 ms |
5596 KB |
Output is correct |
15 |
Correct |
39 ms |
6100 KB |
Output is correct |
16 |
Correct |
32 ms |
3588 KB |
Output is correct |
17 |
Correct |
197 ms |
23784 KB |
Output is correct |
18 |
Correct |
163 ms |
23604 KB |
Output is correct |
19 |
Correct |
139 ms |
22212 KB |
Output is correct |
20 |
Correct |
123 ms |
20584 KB |
Output is correct |
21 |
Correct |
317 ms |
48660 KB |
Output is correct |
22 |
Correct |
356 ms |
46916 KB |
Output is correct |
23 |
Correct |
379 ms |
40192 KB |
Output is correct |
24 |
Correct |
362 ms |
47768 KB |
Output is correct |
25 |
Correct |
757 ms |
93296 KB |
Output is correct |
26 |
Correct |
598 ms |
112080 KB |
Output is correct |
27 |
Correct |
794 ms |
110884 KB |
Output is correct |
28 |
Correct |
943 ms |
106596 KB |
Output is correct |
29 |
Correct |
947 ms |
106932 KB |
Output is correct |
30 |
Correct |
868 ms |
106112 KB |
Output is correct |
31 |
Correct |
722 ms |
53280 KB |
Output is correct |
32 |
Correct |
739 ms |
108900 KB |
Output is correct |