#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define pii pair<int, int>
const int MX = 4005;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int INF;
char a[MX][MX];
int d[MX][MX];
int n, m;
bool valid(int x, int y){
return (x >= 1 && x <= n && y >= 1 && y <= m);
}
int bfs(int x, int y){
memset(d, 63, sizeof d);
INF = d[0][0];
d[1][1] = 1;
deque<pii> q;
q.push_front({1, 1});
int ans = 1;
while(q.size()){
int x, y;
tie(x, y) = q.front();
q.pop_front();
ans = max(ans, d[x][y]);
for(int i=0; i<4; ++i){
int u = x + dx[i];
int v = y + dy[i];
int c = (a[x][y] != a[u][v]);
if(valid(u, v) && a[u][v] != '.' && d[u][v] == INF){
d[u][v] = d[x][y] + c;
if(c == 0){
q.push_front({u, v});
}
else{
q.push_back({u, v});
}
}
}
}
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("main.inp","r",stdin);
// freopen("main.out","w",stdout);
cin>>n>>m;
for(int i=1; i<=n; ++i){
for(int j=1; j<=m; ++j){
cin>>a[i][j];
}
}
cout<<bfs(1, 1);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
130004 KB |
Output is correct |
2 |
Correct |
27 ms |
127836 KB |
Output is correct |
3 |
Correct |
27 ms |
127820 KB |
Output is correct |
4 |
Correct |
33 ms |
130272 KB |
Output is correct |
5 |
Correct |
27 ms |
127824 KB |
Output is correct |
6 |
Correct |
26 ms |
127836 KB |
Output is correct |
7 |
Correct |
26 ms |
127640 KB |
Output is correct |
8 |
Correct |
26 ms |
127816 KB |
Output is correct |
9 |
Correct |
26 ms |
127776 KB |
Output is correct |
10 |
Correct |
27 ms |
127728 KB |
Output is correct |
11 |
Correct |
27 ms |
127828 KB |
Output is correct |
12 |
Correct |
29 ms |
127824 KB |
Output is correct |
13 |
Correct |
27 ms |
127824 KB |
Output is correct |
14 |
Correct |
28 ms |
127832 KB |
Output is correct |
15 |
Correct |
34 ms |
130128 KB |
Output is correct |
16 |
Correct |
35 ms |
130484 KB |
Output is correct |
17 |
Correct |
33 ms |
130132 KB |
Output is correct |
18 |
Correct |
31 ms |
130396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
140876 KB |
Output is correct |
2 |
Correct |
64 ms |
133460 KB |
Output is correct |
3 |
Correct |
263 ms |
157264 KB |
Output is correct |
4 |
Correct |
92 ms |
137552 KB |
Output is correct |
5 |
Correct |
163 ms |
146744 KB |
Output is correct |
6 |
Correct |
640 ms |
184848 KB |
Output is correct |
7 |
Correct |
32 ms |
141660 KB |
Output is correct |
8 |
Correct |
31 ms |
140860 KB |
Output is correct |
9 |
Correct |
25 ms |
127832 KB |
Output is correct |
10 |
Correct |
25 ms |
127836 KB |
Output is correct |
11 |
Correct |
31 ms |
141396 KB |
Output is correct |
12 |
Correct |
26 ms |
127832 KB |
Output is correct |
13 |
Correct |
63 ms |
133312 KB |
Output is correct |
14 |
Correct |
47 ms |
130644 KB |
Output is correct |
15 |
Correct |
48 ms |
132776 KB |
Output is correct |
16 |
Correct |
42 ms |
128344 KB |
Output is correct |
17 |
Correct |
119 ms |
138020 KB |
Output is correct |
18 |
Correct |
105 ms |
137812 KB |
Output is correct |
19 |
Correct |
95 ms |
137544 KB |
Output is correct |
20 |
Correct |
81 ms |
137276 KB |
Output is correct |
21 |
Correct |
166 ms |
149076 KB |
Output is correct |
22 |
Correct |
158 ms |
146896 KB |
Output is correct |
23 |
Correct |
214 ms |
143792 KB |
Output is correct |
24 |
Correct |
165 ms |
148792 KB |
Output is correct |
25 |
Correct |
516 ms |
157308 KB |
Output is correct |
26 |
Correct |
455 ms |
252420 KB |
Output is correct |
27 |
Correct |
549 ms |
210364 KB |
Output is correct |
28 |
Correct |
650 ms |
184648 KB |
Output is correct |
29 |
Correct |
630 ms |
181000 KB |
Output is correct |
30 |
Correct |
594 ms |
192424 KB |
Output is correct |
31 |
Correct |
436 ms |
150956 KB |
Output is correct |
32 |
Correct |
482 ms |
188248 KB |
Output is correct |