#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
template<typename T> bool chkmin(T &a, T b){return b < a ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return b > a ? a = b, 1 : 0;}
const int MAXN = 4e3 + 10, inf = INT_MAX;
const int mv[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int n, m;
char g[MAXN][MAXN];
bool valid(int x, int y){
return x >= 0 && x < n && y >= 0 && y < m && g[x][y] != '.';
}
inline int id(int x, int y){ return x * m + y; }
inline int x(int id){ return id / m; }
inline int y(int id){ return id % m; }
deque<int> q;
int d[MAXN * MAXN];
int bfs(){
for(int i = 0; i < n * m; i++)
d[i] = inf;
d[0] = 1; q.pb(0);
int ans = 0;
while(!q.empty()){
int u = q.front(); q.pop_front();
chkmax(ans, d[u]);
for(int k = 0; k < 4; k++){ // implicit edges
int nx = x(u) + mv[k][0];
int ny = y(u) + mv[k][1];
int v = id(nx, ny);
if(valid(nx, ny) && d[v] == inf){
if(g[x(u)][y(u)] == g[nx][ny]){ // 0
d[v] = d[u];
q.pf(v);
}else{
d[v] = d[u] + 1;
q.pb(v);
}
}
}
}
return ans;
}
int main(){
// input
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
scanf(" %c", &g[i][j]);
// 0/1 bfs
int track = bfs();
printf("%d\n", track);
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
tracks.cpp:54:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf(" %c", &g[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
3324 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
12 ms |
2996 KB |
Output is correct |
5 |
Correct |
5 ms |
1740 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
5 ms |
1596 KB |
Output is correct |
11 |
Correct |
4 ms |
1228 KB |
Output is correct |
12 |
Correct |
8 ms |
1848 KB |
Output is correct |
13 |
Correct |
6 ms |
1832 KB |
Output is correct |
14 |
Correct |
6 ms |
1720 KB |
Output is correct |
15 |
Correct |
19 ms |
3448 KB |
Output is correct |
16 |
Correct |
20 ms |
3320 KB |
Output is correct |
17 |
Correct |
16 ms |
3312 KB |
Output is correct |
18 |
Correct |
12 ms |
3020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
15416 KB |
Output is correct |
2 |
Correct |
96 ms |
12836 KB |
Output is correct |
3 |
Correct |
804 ms |
94204 KB |
Output is correct |
4 |
Correct |
202 ms |
25924 KB |
Output is correct |
5 |
Correct |
505 ms |
56076 KB |
Output is correct |
6 |
Correct |
1275 ms |
100900 KB |
Output is correct |
7 |
Correct |
9 ms |
16204 KB |
Output is correct |
8 |
Correct |
9 ms |
15428 KB |
Output is correct |
9 |
Correct |
5 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
9 ms |
15940 KB |
Output is correct |
12 |
Correct |
2 ms |
972 KB |
Output is correct |
13 |
Correct |
96 ms |
12832 KB |
Output is correct |
14 |
Correct |
55 ms |
8248 KB |
Output is correct |
15 |
Correct |
54 ms |
9008 KB |
Output is correct |
16 |
Correct |
46 ms |
4932 KB |
Output is correct |
17 |
Correct |
263 ms |
28028 KB |
Output is correct |
18 |
Correct |
213 ms |
27588 KB |
Output is correct |
19 |
Correct |
199 ms |
26052 KB |
Output is correct |
20 |
Correct |
178 ms |
23876 KB |
Output is correct |
21 |
Correct |
467 ms |
58004 KB |
Output is correct |
22 |
Correct |
510 ms |
56004 KB |
Output is correct |
23 |
Correct |
469 ms |
48096 KB |
Output is correct |
24 |
Correct |
481 ms |
57096 KB |
Output is correct |
25 |
Correct |
897 ms |
94392 KB |
Output is correct |
26 |
Correct |
996 ms |
99012 KB |
Output is correct |
27 |
Correct |
1214 ms |
103420 KB |
Output is correct |
28 |
Correct |
1259 ms |
100912 KB |
Output is correct |
29 |
Correct |
1289 ms |
99700 KB |
Output is correct |
30 |
Correct |
1297 ms |
101004 KB |
Output is correct |
31 |
Correct |
973 ms |
63196 KB |
Output is correct |
32 |
Correct |
1226 ms |
102584 KB |
Output is correct |