#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
vector<array<int, 2>> del = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
bool check(int x, int y, int r, int c){
return x >= 0 && x < r && y >= 0 && y < c;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int h, w;
cin >> h >> w;
char mat[h][w];
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
cin >> mat[i][j];
deque<array<int, 2>> q;
q.push_back({0, 0});
int d[h][w];
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
d[i][j] = INF;
d[0][0] = 1;
while(!q.empty()){
array<int, 2> cur = q.front();
q.pop_front();
int x = cur[0], y = cur[1];
for(auto &[dx, dy] : del){
if(!check(x+dx, y+dy, h, w))
continue;
if(mat[x+dx][y+dy] == '.')
continue;
int nxt = d[x][y] + (mat[x+dx][y+dy] == mat[x][y] ? 0 : 1);
if(d[x+dx][y+dy] <= nxt)
continue;
if(mat[x+dx][y+dy] == mat[x][y])
q.push_front({x+dx, y+dy});
else q.push_back({x+dx, y+dy});
d[x+dx][y+dy] = nxt;
}
}
int ans = 0;
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++){
if(mat[i][j] == '.')
continue;
ans = max(ans, d[i][j]);
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
9 ms |
1516 KB |
Output is correct |
5 |
Correct |
4 ms |
876 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
3 ms |
748 KB |
Output is correct |
11 |
Correct |
3 ms |
620 KB |
Output is correct |
12 |
Correct |
6 ms |
876 KB |
Output is correct |
13 |
Correct |
4 ms |
876 KB |
Output is correct |
14 |
Correct |
3 ms |
876 KB |
Output is correct |
15 |
Correct |
13 ms |
1772 KB |
Output is correct |
16 |
Correct |
15 ms |
1772 KB |
Output is correct |
17 |
Correct |
11 ms |
1772 KB |
Output is correct |
18 |
Correct |
9 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
61 ms |
9452 KB |
Output is correct |
3 |
Correct |
442 ms |
94316 KB |
Output is correct |
4 |
Correct |
119 ms |
22380 KB |
Output is correct |
5 |
Correct |
287 ms |
53100 KB |
Output is correct |
6 |
Correct |
902 ms |
108656 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
63 ms |
9452 KB |
Output is correct |
14 |
Correct |
34 ms |
5612 KB |
Output is correct |
15 |
Correct |
32 ms |
6124 KB |
Output is correct |
16 |
Correct |
28 ms |
4076 KB |
Output is correct |
17 |
Correct |
163 ms |
24300 KB |
Output is correct |
18 |
Correct |
144 ms |
23996 KB |
Output is correct |
19 |
Correct |
132 ms |
22380 KB |
Output is correct |
20 |
Correct |
104 ms |
20588 KB |
Output is correct |
21 |
Correct |
263 ms |
55020 KB |
Output is correct |
22 |
Correct |
286 ms |
53100 KB |
Output is correct |
23 |
Correct |
312 ms |
45804 KB |
Output is correct |
24 |
Correct |
262 ms |
53740 KB |
Output is correct |
25 |
Correct |
655 ms |
94444 KB |
Output is correct |
26 |
Correct |
575 ms |
122364 KB |
Output is correct |
27 |
Correct |
778 ms |
107696 KB |
Output is correct |
28 |
Correct |
891 ms |
108716 KB |
Output is correct |
29 |
Correct |
880 ms |
106916 KB |
Output is correct |
30 |
Correct |
823 ms |
107896 KB |
Output is correct |
31 |
Correct |
674 ms |
60908 KB |
Output is correct |
32 |
Correct |
708 ms |
108828 KB |
Output is correct |