// This template is based on - Baltic OI 2013 - Tracks in the Snow
// this is basically dijstra, but seems pretty dang cool
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {-1, 1, 0, 0};
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int h, w;
cin >> h >> w;
vector<vector<char>> board(h+5, vector<char>(w+5));
vector<vector<int>> level(h+5, vector<int>(w+5, -1));
for (int i = 1; i <= h; i++){
for (int j = 1; j <= w; j++){
cin >> board[i][j];
}
}
deque<pair<int, int>> q;
q.push_back({1, 1});
level[1][1] = 1;
int ans = 0;
while (!q.empty()){
pair<int, int> u = q.front();
q.pop_front();
int x = u.first, y = u.second;
ans = max(ans, level[y][x]);
for (int k = 0; k < 4; k++){
int tx = x + dx[k], ty = y + dy[k];
if (tx <= 0 || ty <= 0 || tx > w || ty > h || board[ty][tx] == '.' || level[ty][tx] != -1) continue;
if (board[y][x] == board[ty][tx]){
level[ty][tx] = level[y][x];
q.push_front({tx, ty});
}
else{
level[ty][tx] = level[y][x] + 1;
q.push_back({tx, ty});
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1884 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
0 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 |
860 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 |
976 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
6 ms |
1884 KB |
Output is correct |
16 |
Correct |
12 ms |
1936 KB |
Output is correct |
17 |
Correct |
5 ms |
1924 KB |
Output is correct |
18 |
Correct |
4 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
29 ms |
9780 KB |
Output is correct |
3 |
Correct |
202 ms |
94800 KB |
Output is correct |
4 |
Correct |
63 ms |
22716 KB |
Output is correct |
5 |
Correct |
159 ms |
53420 KB |
Output is correct |
6 |
Correct |
451 ms |
109008 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
968 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
30 ms |
9656 KB |
Output is correct |
14 |
Correct |
16 ms |
5724 KB |
Output is correct |
15 |
Correct |
13 ms |
6236 KB |
Output is correct |
16 |
Correct |
22 ms |
4184 KB |
Output is correct |
17 |
Correct |
79 ms |
24472 KB |
Output is correct |
18 |
Correct |
50 ms |
24156 KB |
Output is correct |
19 |
Correct |
55 ms |
22616 KB |
Output is correct |
20 |
Correct |
47 ms |
20816 KB |
Output is correct |
21 |
Correct |
118 ms |
55492 KB |
Output is correct |
22 |
Correct |
153 ms |
53560 KB |
Output is correct |
23 |
Correct |
159 ms |
46164 KB |
Output is correct |
24 |
Correct |
117 ms |
54180 KB |
Output is correct |
25 |
Correct |
214 ms |
94948 KB |
Output is correct |
26 |
Correct |
233 ms |
122980 KB |
Output is correct |
27 |
Correct |
306 ms |
108256 KB |
Output is correct |
28 |
Correct |
419 ms |
109032 KB |
Output is correct |
29 |
Correct |
411 ms |
107276 KB |
Output is correct |
30 |
Correct |
381 ms |
108456 KB |
Output is correct |
31 |
Correct |
313 ms |
61264 KB |
Output is correct |
32 |
Correct |
290 ms |
109256 KB |
Output is correct |