#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
string s[4005];
bool vis[4005][4005];
int ans=0;
int dx[4]={-1,1,0,0}, dy[4]={0,0,-1,1};
deque<pair<int,int>> q;
void solve(){
// freopen("timeline.in","r",stdin);
// freopen("timeline.out","w",stdout);
cin>>n>>m;
for(int i=0; i<n; i++){
cin>>s[i];
}
vis[0][0]=1;
q.push_back({0,0});
char c=s[0][0];
while(!q.empty()){
int x=q.front().first, y=q.front().second;
q.pop_front();
// cout<<x<<" "<<y<<endl;
if(s[x][y]!=c) ans++;
c=s[x][y];
for(int i=0; i<4; i++){
int nx=x+dx[i], ny=y+dy[i];
if(nx>=0 and nx<n and ny>=0 and ny<m and !vis[nx][ny] and s[nx][ny]!='.'){
if(s[nx][ny]!=c){
q.push_back({nx,ny});
}
else{
q.push_front({nx,ny});
}
vis[nx][ny]=1;
}
}
}
cout<<ans+1<<endl;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(NULL);
int t=1;
// cin>>t;
while(t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
5 ms |
3168 KB |
Output is correct |
5 |
Correct |
2 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
1624 KB |
Output is correct |
11 |
Correct |
3 ms |
1372 KB |
Output is correct |
12 |
Correct |
4 ms |
1884 KB |
Output is correct |
13 |
Correct |
2 ms |
1884 KB |
Output is correct |
14 |
Correct |
3 ms |
1884 KB |
Output is correct |
15 |
Correct |
8 ms |
3024 KB |
Output is correct |
16 |
Correct |
9 ms |
2908 KB |
Output is correct |
17 |
Correct |
7 ms |
2908 KB |
Output is correct |
18 |
Correct |
7 ms |
3008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
15448 KB |
Output is correct |
2 |
Correct |
38 ms |
8660 KB |
Output is correct |
3 |
Correct |
143 ms |
49552 KB |
Output is correct |
4 |
Correct |
41 ms |
15576 KB |
Output is correct |
5 |
Correct |
113 ms |
30720 KB |
Output is correct |
6 |
Correct |
437 ms |
75760 KB |
Output is correct |
7 |
Correct |
8 ms |
16292 KB |
Output is correct |
8 |
Correct |
7 ms |
15448 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
9 ms |
15964 KB |
Output is correct |
12 |
Correct |
1 ms |
1144 KB |
Output is correct |
13 |
Correct |
35 ms |
8660 KB |
Output is correct |
14 |
Correct |
16 ms |
5984 KB |
Output is correct |
15 |
Correct |
11 ms |
6492 KB |
Output is correct |
16 |
Correct |
16 ms |
3500 KB |
Output is correct |
17 |
Correct |
89 ms |
16760 KB |
Output is correct |
18 |
Correct |
46 ms |
16540 KB |
Output is correct |
19 |
Correct |
43 ms |
15568 KB |
Output is correct |
20 |
Correct |
45 ms |
14448 KB |
Output is correct |
21 |
Correct |
116 ms |
31876 KB |
Output is correct |
22 |
Correct |
106 ms |
30844 KB |
Output is correct |
23 |
Correct |
149 ms |
26328 KB |
Output is correct |
24 |
Correct |
93 ms |
31416 KB |
Output is correct |
25 |
Correct |
267 ms |
49552 KB |
Output is correct |
26 |
Correct |
276 ms |
140004 KB |
Output is correct |
27 |
Correct |
375 ms |
84508 KB |
Output is correct |
28 |
Correct |
425 ms |
75816 KB |
Output is correct |
29 |
Correct |
440 ms |
76556 KB |
Output is correct |
30 |
Correct |
370 ms |
77536 KB |
Output is correct |
31 |
Correct |
326 ms |
34868 KB |
Output is correct |
32 |
Correct |
304 ms |
80332 KB |
Output is correct |