#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
struct P{
ll x, y;
};
void dbg_out() { cerr << endl; }
template <typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
const int N = 4001;
char grid[N][N];
int dist[N][N];
int h, w;
int valid(int x, int y){
return (0 <= x && x < h) && (0 <= y && y < w);
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
signed main(){
fastio;
cin >> h >> w;
for (int i=0; i<h; i++){
for (int j=0; j<w; j++){
cin >> grid[i][j];
}
}
memset(dist, -1, sizeof(dist));
int ans=1;
queue<pair<int, int>> q0, q1;
dist[0][0] = 1;
q0.push({0, 0});
int x, y;
int ctr=0;
while (!q0.empty() || !q1.empty()){
while (!q0.empty()){
tie(x, y) = q0.front(); q0.pop();
ans = max(ans, dist[x][y]);
for (int i=0; i<4; i++){
int a, b;
a = dx[i]+x;
b = dy[i]+y;
if (!valid(a, b) || grid[a][b] == '.' || dist[a][b] != -1) continue;
dist[a][b] = dist[x][y];
if (grid[a][b] != grid[x][y]){
dist[a][b]++;
q1.push({a, b});
}
else q0.push({a, b});
}
}
swap(q0, q1);
}
cout << ans << '\n';
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:46:9: warning: unused variable 'ctr' [-Wunused-variable]
46 | int ctr=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
66140 KB |
Output is correct |
2 |
Correct |
6 ms |
64092 KB |
Output is correct |
3 |
Correct |
9 ms |
64204 KB |
Output is correct |
4 |
Correct |
10 ms |
66396 KB |
Output is correct |
5 |
Correct |
8 ms |
64212 KB |
Output is correct |
6 |
Correct |
7 ms |
64092 KB |
Output is correct |
7 |
Correct |
8 ms |
64164 KB |
Output is correct |
8 |
Correct |
7 ms |
64156 KB |
Output is correct |
9 |
Correct |
7 ms |
64228 KB |
Output is correct |
10 |
Correct |
9 ms |
64248 KB |
Output is correct |
11 |
Correct |
8 ms |
64092 KB |
Output is correct |
12 |
Correct |
9 ms |
64212 KB |
Output is correct |
13 |
Correct |
9 ms |
64088 KB |
Output is correct |
14 |
Correct |
9 ms |
64204 KB |
Output is correct |
15 |
Correct |
15 ms |
66196 KB |
Output is correct |
16 |
Correct |
15 ms |
66136 KB |
Output is correct |
17 |
Correct |
13 ms |
66320 KB |
Output is correct |
18 |
Correct |
13 ms |
66392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
78496 KB |
Output is correct |
2 |
Correct |
38 ms |
68244 KB |
Output is correct |
3 |
Correct |
205 ms |
78544 KB |
Output is correct |
4 |
Correct |
57 ms |
72276 KB |
Output is correct |
5 |
Correct |
150 ms |
76368 KB |
Output is correct |
6 |
Correct |
533 ms |
88908 KB |
Output is correct |
7 |
Correct |
10 ms |
78772 KB |
Output is correct |
8 |
Correct |
11 ms |
78556 KB |
Output is correct |
9 |
Correct |
9 ms |
64088 KB |
Output is correct |
10 |
Correct |
7 ms |
64092 KB |
Output is correct |
11 |
Correct |
9 ms |
78560 KB |
Output is correct |
12 |
Correct |
8 ms |
64092 KB |
Output is correct |
13 |
Correct |
38 ms |
68320 KB |
Output is correct |
14 |
Correct |
25 ms |
68188 KB |
Output is correct |
15 |
Correct |
21 ms |
68188 KB |
Output is correct |
16 |
Correct |
21 ms |
66324 KB |
Output is correct |
17 |
Correct |
93 ms |
72392 KB |
Output is correct |
18 |
Correct |
63 ms |
72272 KB |
Output is correct |
19 |
Correct |
58 ms |
72280 KB |
Output is correct |
20 |
Correct |
51 ms |
70232 KB |
Output is correct |
21 |
Correct |
122 ms |
76368 KB |
Output is correct |
22 |
Correct |
148 ms |
76368 KB |
Output is correct |
23 |
Correct |
163 ms |
74452 KB |
Output is correct |
24 |
Correct |
121 ms |
76628 KB |
Output is correct |
25 |
Correct |
241 ms |
78676 KB |
Output is correct |
26 |
Correct |
246 ms |
78416 KB |
Output is correct |
27 |
Correct |
389 ms |
80468 KB |
Output is correct |
28 |
Correct |
556 ms |
88884 KB |
Output is correct |
29 |
Correct |
510 ms |
87244 KB |
Output is correct |
30 |
Correct |
491 ms |
86244 KB |
Output is correct |
31 |
Correct |
384 ms |
76984 KB |
Output is correct |
32 |
Correct |
323 ms |
79400 KB |
Output is correct |