#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MN=4e3+2;
int H,W,ans=0;
char gr[MN][MN];
bool vis[MN][MN];
bool chk(int r,int c) {
return r>0&&c>0&&r<=H&&c<=W&&gr[r][c]!='.'&&!vis[r][c];
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);
cin>>H>>W;
for(int i=1;i<=H;++i) cin>>(gr[i]+1);
queue<pii> q,nq;
q.push({1,1});
vis[1][1]=1;
while(1) {
++ans;
while(!q.empty()) {
int r=q.front().first,c=q.front().second;
q.pop();
if(chk(r+1,c)) {
vis[r+1][c]=1;
if(gr[r][c]==gr[r+1][c]) q.push({r+1,c});
else nq.push({r+1,c});
}
if(chk(r-1,c)) {
vis[r-1][c]=1;
if(gr[r][c]==gr[r-1][c]) q.push({r-1,c});
else nq.push({r-1,c});
}
if(chk(r,c+1)) {
vis[r][c+1]=1;
if(gr[r][c]==gr[r][c+1]) q.push({r,c+1});
else nq.push({r,c+1});
}
if(chk(r,c-1)) {
vis[r][c-1]=1;
if(gr[r][c]==gr[r][c-1]) q.push({r,c-1});
else nq.push({r,c-1});
}
}
swap(nq,q);
if(q.empty()) break;
}
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
4436 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
712 KB |
Output is correct |
4 |
Correct |
7 ms |
4436 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
3 ms |
2296 KB |
Output is correct |
11 |
Correct |
3 ms |
1884 KB |
Output is correct |
12 |
Correct |
5 ms |
2644 KB |
Output is correct |
13 |
Correct |
3 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
10 ms |
4532 KB |
Output is correct |
16 |
Correct |
12 ms |
4436 KB |
Output is correct |
17 |
Correct |
7 ms |
4308 KB |
Output is correct |
18 |
Correct |
8 ms |
4392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
30288 KB |
Output is correct |
2 |
Correct |
43 ms |
11652 KB |
Output is correct |
3 |
Correct |
177 ms |
40304 KB |
Output is correct |
4 |
Correct |
48 ms |
18760 KB |
Output is correct |
5 |
Correct |
121 ms |
32412 KB |
Output is correct |
6 |
Correct |
761 ms |
50456 KB |
Output is correct |
7 |
Correct |
17 ms |
31668 KB |
Output is correct |
8 |
Correct |
15 ms |
30292 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
15 ms |
31060 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
36 ms |
11644 KB |
Output is correct |
14 |
Correct |
24 ms |
8340 KB |
Output is correct |
15 |
Correct |
17 ms |
9044 KB |
Output is correct |
16 |
Correct |
19 ms |
4052 KB |
Output is correct |
17 |
Correct |
96 ms |
20084 KB |
Output is correct |
18 |
Correct |
60 ms |
19900 KB |
Output is correct |
19 |
Correct |
49 ms |
18708 KB |
Output is correct |
20 |
Correct |
41 ms |
17368 KB |
Output is correct |
21 |
Correct |
93 ms |
33368 KB |
Output is correct |
22 |
Correct |
117 ms |
32600 KB |
Output is correct |
23 |
Correct |
184 ms |
27608 KB |
Output is correct |
24 |
Correct |
97 ms |
33528 KB |
Output is correct |
25 |
Correct |
280 ms |
40348 KB |
Output is correct |
26 |
Correct |
370 ms |
36384 KB |
Output is correct |
27 |
Correct |
525 ms |
41916 KB |
Output is correct |
28 |
Correct |
721 ms |
50388 KB |
Output is correct |
29 |
Correct |
732 ms |
48812 KB |
Output is correct |
30 |
Correct |
628 ms |
47516 KB |
Output is correct |
31 |
Correct |
536 ms |
34544 KB |
Output is correct |
32 |
Correct |
473 ms |
41012 KB |
Output is correct |