#include <bits/stdc++.h>
using namespace std;
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int N, M, dist[4000][4000], ans = 1;
string grid[4000];
int main (void) {
iostream::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
//vector<string> grid(N);
for (int i = 0; i < N; i++)
cin >> grid[i];
//vector<vector<int>> dist(N, vector<int>(M, 0));
deque<pair<int, int>> bfs;
bfs.push_back({0, 0});
dist[0][0] = 1;
while (!bfs.empty()) {
int x = bfs.front().first, y = bfs.front().second;
bfs.pop_front();
ans = max(ans, dist[x][y]);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
/*if (nx < 0 || nx >= N || ny < 0 || ny >= M)
continue;
if (dist[nx][ny] != 0 || grid[nx][ny] == '.')
continue;
pair<int, int> next = {nx, ny};
if (grid[x][y] != grid[nx][ny]) {
dist[nx][ny] = dist[x][y] + 1;
bfs.push_back({nx, ny});
} else {
dist[nx][ny] = dist[x][y];
bfs.push_front({nx, ny});
}*/
if (nx >= 0 && nx < N && ny >= 0 && ny < M && grid[nx][ny] != '.' && dist[nx][ny] == 0) {
if (grid[nx][ny] != grid[x][y]) {
dist[nx][ny] = dist[x][y] + 1;
bfs.push_back({nx, ny});
} else {
dist[nx][ny] = dist[x][y];
bfs.push_front({nx, ny});
}
}
}
}
/*for (auto x: dist) {
for (auto y: x) {
cout << y << " ";
}
cout << endl;
}*/
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3676 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
5 ms |
3480 KB |
Output is correct |
5 |
Correct |
2 ms |
1884 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
1628 KB |
Output is correct |
11 |
Correct |
1 ms |
1628 KB |
Output is correct |
12 |
Correct |
4 ms |
2136 KB |
Output is correct |
13 |
Correct |
2 ms |
1880 KB |
Output is correct |
14 |
Correct |
2 ms |
1880 KB |
Output is correct |
15 |
Correct |
8 ms |
3416 KB |
Output is correct |
16 |
Correct |
8 ms |
3676 KB |
Output is correct |
17 |
Correct |
6 ms |
3420 KB |
Output is correct |
18 |
Correct |
4 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15964 KB |
Output is correct |
2 |
Correct |
26 ms |
10368 KB |
Output is correct |
3 |
Correct |
146 ms |
59988 KB |
Output is correct |
4 |
Correct |
43 ms |
25660 KB |
Output is correct |
5 |
Correct |
111 ms |
42836 KB |
Output is correct |
6 |
Correct |
393 ms |
94608 KB |
Output is correct |
7 |
Correct |
7 ms |
16472 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
7 ms |
16220 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
25 ms |
10332 KB |
Output is correct |
14 |
Correct |
15 ms |
7004 KB |
Output is correct |
15 |
Correct |
11 ms |
9304 KB |
Output is correct |
16 |
Correct |
13 ms |
4440 KB |
Output is correct |
17 |
Correct |
74 ms |
21584 KB |
Output is correct |
18 |
Correct |
51 ms |
28184 KB |
Output is correct |
19 |
Correct |
44 ms |
25688 KB |
Output is correct |
20 |
Correct |
35 ms |
19036 KB |
Output is correct |
21 |
Correct |
86 ms |
41452 KB |
Output is correct |
22 |
Correct |
110 ms |
42836 KB |
Output is correct |
23 |
Correct |
132 ms |
34644 KB |
Output is correct |
24 |
Correct |
86 ms |
38636 KB |
Output is correct |
25 |
Correct |
280 ms |
80724 KB |
Output is correct |
26 |
Correct |
221 ms |
119008 KB |
Output is correct |
27 |
Correct |
293 ms |
107260 KB |
Output is correct |
28 |
Correct |
391 ms |
94608 KB |
Output is correct |
29 |
Correct |
378 ms |
92716 KB |
Output is correct |
30 |
Correct |
345 ms |
97240 KB |
Output is correct |
31 |
Correct |
317 ms |
62156 KB |
Output is correct |
32 |
Correct |
253 ms |
92696 KB |
Output is correct |