/*
BoopyTheNoob, 29.10.2023, 20:55 +8
Somehow there's an error if the variables aren't declared globally.
*/
#include <bits/stdc++.h>
using namespace std;
int main (void) {
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int N, M;
iostream::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
//vector<string> grid(N);
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});
int dist[N][M], ans = 1;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
dist[i][j] = 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 |
7 ms |
1628 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
1368 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
788 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
6 ms |
1628 KB |
Output is correct |
16 |
Correct |
7 ms |
1628 KB |
Output is correct |
17 |
Correct |
5 ms |
1628 KB |
Output is correct |
18 |
Correct |
4 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
24 ms |
8028 KB |
Output is correct |
3 |
Correct |
165 ms |
80724 KB |
Output is correct |
4 |
Correct |
39 ms |
19032 KB |
Output is correct |
5 |
Correct |
122 ms |
45472 KB |
Output is correct |
6 |
Correct |
384 ms |
94696 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
848 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
28 ms |
8260 KB |
Output is correct |
14 |
Correct |
14 ms |
4956 KB |
Output is correct |
15 |
Correct |
10 ms |
5220 KB |
Output is correct |
16 |
Correct |
17 ms |
3676 KB |
Output is correct |
17 |
Correct |
64 ms |
20572 KB |
Output is correct |
18 |
Correct |
62 ms |
20316 KB |
Output is correct |
19 |
Correct |
57 ms |
19108 KB |
Output is correct |
20 |
Correct |
36 ms |
17496 KB |
Output is correct |
21 |
Correct |
90 ms |
47024 KB |
Output is correct |
22 |
Correct |
111 ms |
45400 KB |
Output is correct |
23 |
Correct |
130 ms |
39384 KB |
Output is correct |
24 |
Correct |
91 ms |
45876 KB |
Output is correct |
25 |
Correct |
328 ms |
80844 KB |
Output is correct |
26 |
Correct |
243 ms |
111968 KB |
Output is correct |
27 |
Correct |
304 ms |
107440 KB |
Output is correct |
28 |
Correct |
424 ms |
94476 KB |
Output is correct |
29 |
Correct |
385 ms |
92804 KB |
Output is correct |
30 |
Correct |
342 ms |
97312 KB |
Output is correct |
31 |
Correct |
304 ms |
52140 KB |
Output is correct |
32 |
Correct |
248 ms |
96012 KB |
Output is correct |