#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
#define rep(i, x, y) for(int i = x; i <= y; ++i)
#define repi(i,x,y) for(int i = x; i >= y; --i)
#define ci(x) int x; cin>> x
#define TC(t) ci(t); while(t--)
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define cii(x, y) ci(x); ci(y)
#define ciii(x, y, z) ci(x); ci(y); ci(z)
#define mp make_pair
//#define int long long
typedef long long ll;
typedef vector<int> vi;
const int N = 4e3 + 5;
const int mod = 1e9 + 7;
const int mod1 = 1e9 + 9;
const int pi = 31;
const int inf = 1e9 + 5;
const int block = 330;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
void readfile(){
#ifdef ONLINE_JUDGE
#else
freopen("text.inp", "r", stdin);
#endif // ONLINE_JUDGE
// freopen("248.in", "r", stdin);
// freopen("248.out", "w", stdout);
}
int n, m;
char a[N][N];
void inp(){
cin >> n >> m;
rep(i,1,n)
rep(j,1,m)
cin >> a[i][j];
}
queue<pair<int,int> > q[2];
int d[N][N], vis[N][N];
void bfs(int x, int y){
rep(i,1,n)
rep(j,1,m)
d[i][j] = inf;
d[x][y] = 1;
q[1].push(mp(x, y));
int num = 1, pos = 1;
while(num){
while(q[pos % 2].empty())
++pos;
auto top = q[pos % 2].front();
q[pos % 2].pop(), --num;
if(vis[top.fi][top.se]) continue;
vis[top.fi][top.se] = 1;
rep(i,0,3){
int x = top.fi + dx[i];
int y = top.se + dy[i];
if(1 <= x && x <= n && 1 <= y && y <= m && a[x][y]!='.'){
int cost = (a[x][y] != a[top.fi][top.se]);
if(d[x][y] > d[top.fi][top.se] + cost){
d[x][y] = d[top.fi][top.se] + cost;
q[d[x][y] % 2].push(mp(x, y));
++num;
}
}
}
}
}
void process(){
bfs(1,1);
int res = 0;
rep(i,1,n)
rep(j,1,m)
if(d[i][j]!=inf)
res = max(res, d[i][j]);
cout << res;
}
int main() {
// readfile();
ios_base::sync_with_stdio(0);
cin.tie(0);
// TC(t){
inp();
process();
// }
return 0;
}
Compilation message
tracks.cpp: In function 'void readfile()':
tracks.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen("text.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
8268 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
844 KB |
Output is correct |
4 |
Correct |
12 ms |
7616 KB |
Output is correct |
5 |
Correct |
5 ms |
4428 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
844 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1484 KB |
Output is correct |
10 |
Correct |
4 ms |
3648 KB |
Output is correct |
11 |
Correct |
4 ms |
3020 KB |
Output is correct |
12 |
Correct |
7 ms |
4556 KB |
Output is correct |
13 |
Correct |
4 ms |
4432 KB |
Output is correct |
14 |
Correct |
5 ms |
4564 KB |
Output is correct |
15 |
Correct |
17 ms |
8164 KB |
Output is correct |
16 |
Correct |
20 ms |
8396 KB |
Output is correct |
17 |
Correct |
19 ms |
8040 KB |
Output is correct |
18 |
Correct |
13 ms |
7708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
46412 KB |
Output is correct |
2 |
Correct |
79 ms |
26100 KB |
Output is correct |
3 |
Correct |
396 ms |
135968 KB |
Output is correct |
4 |
Correct |
134 ms |
54808 KB |
Output is correct |
5 |
Correct |
276 ms |
100388 KB |
Output is correct |
6 |
Correct |
1673 ms |
167168 KB |
Output is correct |
7 |
Correct |
24 ms |
48460 KB |
Output is correct |
8 |
Correct |
22 ms |
46364 KB |
Output is correct |
9 |
Correct |
3 ms |
920 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
23 ms |
47556 KB |
Output is correct |
12 |
Correct |
2 ms |
2252 KB |
Output is correct |
13 |
Correct |
79 ms |
26200 KB |
Output is correct |
14 |
Correct |
43 ms |
17612 KB |
Output is correct |
15 |
Correct |
42 ms |
20916 KB |
Output is correct |
16 |
Correct |
30 ms |
9744 KB |
Output is correct |
17 |
Correct |
205 ms |
52772 KB |
Output is correct |
18 |
Correct |
151 ms |
59436 KB |
Output is correct |
19 |
Correct |
139 ms |
54876 KB |
Output is correct |
20 |
Correct |
95 ms |
46024 KB |
Output is correct |
21 |
Correct |
242 ms |
101188 KB |
Output is correct |
22 |
Correct |
274 ms |
100360 KB |
Output is correct |
23 |
Correct |
408 ms |
83124 KB |
Output is correct |
24 |
Correct |
241 ms |
97740 KB |
Output is correct |
25 |
Correct |
708 ms |
156960 KB |
Output is correct |
26 |
Correct |
927 ms |
135704 KB |
Output is correct |
27 |
Correct |
1353 ms |
158696 KB |
Output is correct |
28 |
Correct |
1627 ms |
167084 KB |
Output is correct |
29 |
Correct |
1482 ms |
165576 KB |
Output is correct |
30 |
Correct |
1401 ms |
162124 KB |
Output is correct |
31 |
Correct |
1063 ms |
123764 KB |
Output is correct |
32 |
Correct |
1204 ms |
157680 KB |
Output is correct |