#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000007
#define LOGN 20
#define MAXN 1000005
const int delRow[] = {-1, 0, 0, 1};
const int delCol[] = {0, -1, 1, 0};
int H, W;
vector<string> grid;
int main() {
fast
cin >> H >> W;
grid = vector<string>(H);
for (int i = 0; i < H; i++)
cin >> grid[i];
vector<vector<int>> depth(H, vector<int>(W, 0));
deque<pair<int,int>> dq;
dq.push_back({0, 0});
depth[0][0] = 1;
int ans = 0;
while (dq.size()) {
int row = dq.front().f;
int col = dq.front().s;
ans = max(ans, depth[row][col]);
dq.pop_front();
for (int i = 0; i < 4; i++) {
int nRow = row + delRow[i];
int nCol = col + delCol[i];
if (nRow >= 0 && nRow < H && nCol >= 0 && nCol < W && grid[nRow][nCol] != '.' && depth[nRow][nCol] == 0) {
if (grid[row][col] == grid[nRow][nCol]) {
depth[nRow][nCol] = depth[row][col];
dq.push_front({nRow, nCol});
} else {
depth[nRow][nCol] = depth[row][col] + 1;
dq.push_back({nRow, nCol});
}
}
}
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1732 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
1364 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
804 KB |
Output is correct |
15 |
Correct |
7 ms |
1840 KB |
Output is correct |
16 |
Correct |
8 ms |
1852 KB |
Output is correct |
17 |
Correct |
6 ms |
1784 KB |
Output is correct |
18 |
Correct |
4 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
28 ms |
9340 KB |
Output is correct |
3 |
Correct |
153 ms |
84732 KB |
Output is correct |
4 |
Correct |
52 ms |
21320 KB |
Output is correct |
5 |
Correct |
114 ms |
47948 KB |
Output is correct |
6 |
Correct |
472 ms |
99828 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
27 ms |
9440 KB |
Output is correct |
14 |
Correct |
15 ms |
5716 KB |
Output is correct |
15 |
Correct |
13 ms |
6308 KB |
Output is correct |
16 |
Correct |
14 ms |
4168 KB |
Output is correct |
17 |
Correct |
75 ms |
23128 KB |
Output is correct |
18 |
Correct |
54 ms |
22796 KB |
Output is correct |
19 |
Correct |
44 ms |
21384 KB |
Output is correct |
20 |
Correct |
37 ms |
19516 KB |
Output is correct |
21 |
Correct |
94 ms |
50116 KB |
Output is correct |
22 |
Correct |
126 ms |
48604 KB |
Output is correct |
23 |
Correct |
149 ms |
41744 KB |
Output is correct |
24 |
Correct |
92 ms |
48876 KB |
Output is correct |
25 |
Correct |
356 ms |
85304 KB |
Output is correct |
26 |
Correct |
243 ms |
116104 KB |
Output is correct |
27 |
Correct |
396 ms |
124912 KB |
Output is correct |
28 |
Correct |
570 ms |
99388 KB |
Output is correct |
29 |
Correct |
475 ms |
95464 KB |
Output is correct |
30 |
Correct |
413 ms |
100456 KB |
Output is correct |
31 |
Correct |
409 ms |
52376 KB |
Output is correct |
32 |
Correct |
478 ms |
119496 KB |
Output is correct |