#include <bits/stdc++.h>
using namespace std;
int main (void) {
iostream::sync_with_stdio(false);
cin.tie(0);
int dx[4]{1, -1, 0, 0};
int dy[4]{0, 0, 1, -1};
int N, M;
cin >> N >> M;
//vector<string> grid(N);
string grid[4000];
for (int i = 0; i < N; i++)
cin >> grid[i];
//vector<vector<int>> dist(N, vector<int>(M, 0));
int dist[4000][4000];
deque<pair<int, int>> bfs;
bfs.push_back({0, 0});
dist[0][0] = 1;
int ans = 0;
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 << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
63704 KB |
Output is correct |
2 |
Correct |
32 ms |
63056 KB |
Output is correct |
3 |
Correct |
33 ms |
63108 KB |
Output is correct |
4 |
Correct |
43 ms |
63448 KB |
Output is correct |
5 |
Correct |
33 ms |
63324 KB |
Output is correct |
6 |
Correct |
33 ms |
63092 KB |
Output is correct |
7 |
Correct |
32 ms |
63176 KB |
Output is correct |
8 |
Correct |
33 ms |
63068 KB |
Output is correct |
9 |
Correct |
36 ms |
63124 KB |
Output is correct |
10 |
Correct |
34 ms |
63128 KB |
Output is correct |
11 |
Correct |
34 ms |
63316 KB |
Output is correct |
12 |
Correct |
40 ms |
63320 KB |
Output is correct |
13 |
Correct |
33 ms |
63280 KB |
Output is correct |
14 |
Correct |
34 ms |
63320 KB |
Output is correct |
15 |
Correct |
45 ms |
63612 KB |
Output is correct |
16 |
Correct |
40 ms |
63572 KB |
Output is correct |
17 |
Correct |
37 ms |
63568 KB |
Output is correct |
18 |
Correct |
36 ms |
63572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
63192 KB |
Output is correct |
2 |
Correct |
56 ms |
65852 KB |
Output is correct |
3 |
Correct |
164 ms |
89568 KB |
Output is correct |
4 |
Correct |
70 ms |
69456 KB |
Output is correct |
5 |
Correct |
132 ms |
78112 KB |
Output is correct |
6 |
Correct |
421 ms |
103336 KB |
Output is correct |
7 |
Correct |
33 ms |
63056 KB |
Output is correct |
8 |
Correct |
33 ms |
63068 KB |
Output is correct |
9 |
Correct |
34 ms |
63100 KB |
Output is correct |
10 |
Correct |
37 ms |
63068 KB |
Output is correct |
11 |
Correct |
34 ms |
63052 KB |
Output is correct |
12 |
Correct |
32 ms |
63060 KB |
Output is correct |
13 |
Correct |
55 ms |
65620 KB |
Output is correct |
14 |
Correct |
45 ms |
64812 KB |
Output is correct |
15 |
Correct |
43 ms |
64852 KB |
Output is correct |
16 |
Correct |
44 ms |
64208 KB |
Output is correct |
17 |
Correct |
94 ms |
70044 KB |
Output is correct |
18 |
Correct |
78 ms |
69716 KB |
Output is correct |
19 |
Correct |
70 ms |
69456 KB |
Output is correct |
20 |
Correct |
62 ms |
68944 KB |
Output is correct |
21 |
Correct |
114 ms |
78428 KB |
Output is correct |
22 |
Correct |
131 ms |
78068 KB |
Output is correct |
23 |
Correct |
157 ms |
76188 KB |
Output is correct |
24 |
Correct |
107 ms |
78244 KB |
Output is correct |
25 |
Incorrect |
74 ms |
89448 KB |
Output isn't correct |
26 |
Correct |
235 ms |
133984 KB |
Output is correct |
27 |
Correct |
322 ms |
115964 KB |
Output is correct |
28 |
Correct |
420 ms |
103248 KB |
Output is correct |
29 |
Correct |
415 ms |
101528 KB |
Output is correct |
30 |
Correct |
384 ms |
106784 KB |
Output is correct |
31 |
Correct |
358 ms |
80840 KB |
Output is correct |
32 |
Correct |
259 ms |
104716 KB |
Output is correct |