#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define p3i pair<pii, int>
#define pll pair<ll, ll>
#define p3l pair<pll, ll>
#define lseg L, (L+R)/2, N*2+1
#define rseg (L+R)/2+1, R, N*2+2
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define MN 1000000007
#define fox(k, x) for (int k=0; k<x; ++k)
#define fox1(k, x) for (int k=1; k<=x; ++k)
#define foxr(k, x) for (int k=x-1; k>=0; --k)
#define fox1r(k, x) for (int k=x; k>0; --k)
#define ms multiset
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
int n, m, X, Y, ans;
char g[4005][4005];
bool u[4005][4005];
queue<pii> q[2];
void bfs(){
bool k;
q[0].push(mp(1, 1));
for (ans=0; !q[0].empty(); ans+=k){
k=0;
//cout << "*";
while(!q[0].empty()){
X=q[0].front().x; Y=q[0].front().y; q[0].pop();
if (X<1 || Y<1 || X>n || Y>m) continue;
if (u[X][Y] || g[X][Y]=='.') continue;
u[X][Y]=1;
k=1;
q[g[X+1][Y]!=g[X][Y]].push(mp(X+1, Y));
q[g[X-1][Y]!=g[X][Y]].push(mp(X-1, Y));
q[g[X][Y+1]!=g[X][Y]].push(mp(X, Y+1));
q[g[X][Y-1]!=g[X][Y]].push(mp(X, Y-1));
}
q[0]=q[1];
while(!q[1].empty()) q[1].pop();
}
}
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
fox1(l, n){
fox1(l2, m){
cin >> g[l][l2];
}
}
bfs();
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
33924 KB |
Output is correct |
2 |
Correct |
0 ms |
33516 KB |
Output is correct |
3 |
Correct |
0 ms |
33516 KB |
Output is correct |
4 |
Correct |
13 ms |
34156 KB |
Output is correct |
5 |
Correct |
3 ms |
33516 KB |
Output is correct |
6 |
Correct |
0 ms |
33516 KB |
Output is correct |
7 |
Correct |
0 ms |
33516 KB |
Output is correct |
8 |
Correct |
0 ms |
33516 KB |
Output is correct |
9 |
Correct |
0 ms |
33516 KB |
Output is correct |
10 |
Correct |
3 ms |
33516 KB |
Output is correct |
11 |
Correct |
3 ms |
33648 KB |
Output is correct |
12 |
Correct |
9 ms |
33776 KB |
Output is correct |
13 |
Correct |
3 ms |
33516 KB |
Output is correct |
14 |
Correct |
3 ms |
33516 KB |
Output is correct |
15 |
Correct |
19 ms |
33648 KB |
Output is correct |
16 |
Correct |
26 ms |
33924 KB |
Output is correct |
17 |
Correct |
16 ms |
33648 KB |
Output is correct |
18 |
Correct |
9 ms |
34156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
33648 KB |
Output is correct |
2 |
Correct |
116 ms |
33648 KB |
Output is correct |
3 |
Correct |
599 ms |
33780 KB |
Output is correct |
4 |
Correct |
146 ms |
33648 KB |
Output is correct |
5 |
Correct |
523 ms |
33648 KB |
Output is correct |
6 |
Execution timed out |
2000 ms |
88104 KB |
Execution timed out |
7 |
Correct |
0 ms |
33648 KB |
Output is correct |
8 |
Correct |
3 ms |
33648 KB |
Output is correct |
9 |
Correct |
3 ms |
33648 KB |
Output is correct |
10 |
Correct |
0 ms |
33648 KB |
Output is correct |
11 |
Correct |
3 ms |
33648 KB |
Output is correct |
12 |
Correct |
3 ms |
33516 KB |
Output is correct |
13 |
Correct |
109 ms |
33648 KB |
Output is correct |
14 |
Correct |
49 ms |
33648 KB |
Output is correct |
15 |
Correct |
59 ms |
33516 KB |
Output is correct |
16 |
Correct |
46 ms |
33648 KB |
Output is correct |
17 |
Correct |
313 ms |
33780 KB |
Output is correct |
18 |
Correct |
196 ms |
33648 KB |
Output is correct |
19 |
Correct |
146 ms |
33648 KB |
Output is correct |
20 |
Correct |
149 ms |
33648 KB |
Output is correct |
21 |
Correct |
346 ms |
33648 KB |
Output is correct |
22 |
Correct |
506 ms |
33648 KB |
Output is correct |
23 |
Correct |
646 ms |
33844 KB |
Output is correct |
24 |
Correct |
433 ms |
33648 KB |
Output is correct |
25 |
Correct |
963 ms |
33780 KB |
Output is correct |
26 |
Correct |
1339 ms |
33776 KB |
Output is correct |
27 |
Execution timed out |
2000 ms |
41064 KB |
Execution timed out |
28 |
Execution timed out |
2000 ms |
88104 KB |
Execution timed out |
29 |
Execution timed out |
2000 ms |
80180 KB |
Execution timed out |
30 |
Correct |
1833 ms |
78928 KB |
Output is correct |
31 |
Execution timed out |
2000 ms |
36888 KB |
Execution timed out |
32 |
Correct |
1386 ms |
38532 KB |
Output is correct |