#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int h,w;
cin>>h>>w;
string s[h+5];
int used[h+5][w+5];
memset(used,0,sizeof(used));
for(int i=0;i<h;i++){
cin>>s[i];
}
set<pair<int,pair<int,int>>>st;
st.insert({1,{0,0}});
used[0][0]=1;
queue<pair<int,int>>q[h*w+5];
q[1].push({0,0});
for(int i=1;i<=h*w+1;i++){
if(q[i].size()==0){
cout<<i-1;
return 0;
}
while(!q[i].empty()){
int x=q[i].front().ff,y=q[i].front().ss;
q[i].pop();
if(x>0&&used[x-1][y]==0){
if(s[x-1][y]==s[x][y]){
used[x-1][y]=1;
q[i].push({x-1,y});
}
else if(s[x-1][y]!='.'){
used[x-1][y]=1;
q[i+1].push({x-1,y});
}
}
if(x<h-1&&used[x+1][y]==0){
if(s[x+1][y]==s[x][y]){
used[x+1][y]=1;
q[i].push({x+1,y});
}
else if(s[x+1][y]!='.'){
used[x+1][y]=1;
q[i+1].push({x+1,y});
}
}
if(y>0&&used[x][y-1]==0){
if(s[x][y-1]==s[x][y]){
used[x][y-1]=1;
q[i].push({x,y-1});
}
else if(s[x][y-1]!='.'){
used[x][y-1]=1;
q[i+1].push({x,y-1});
}
}
if(y<w-1&&used[x][y+1]==0){
if(s[x][y+1]==s[x][y]){
used[x][y+1]=1;
q[i].push({x,y+1});
}
else if(s[x][y+1]!='.'){
used[x][y+1]=1;
q[i+1].push({x,y+1});
}
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
163916 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
2004 KB |
Output is correct |
4 |
Correct |
70 ms |
104456 KB |
Output is correct |
5 |
Correct |
34 ms |
56908 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
2004 KB |
Output is correct |
8 |
Correct |
3 ms |
3540 KB |
Output is correct |
9 |
Correct |
4 ms |
6996 KB |
Output is correct |
10 |
Correct |
27 ms |
44420 KB |
Output is correct |
11 |
Correct |
17 ms |
27472 KB |
Output is correct |
12 |
Correct |
42 ms |
59168 KB |
Output is correct |
13 |
Correct |
35 ms |
56940 KB |
Output is correct |
14 |
Correct |
36 ms |
56940 KB |
Output is correct |
15 |
Correct |
106 ms |
169908 KB |
Output is correct |
16 |
Correct |
114 ms |
163840 KB |
Output is correct |
17 |
Correct |
96 ms |
161784 KB |
Output is correct |
18 |
Correct |
72 ms |
104572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
34120 KB |
Output is correct |
2 |
Runtime error |
443 ms |
1048576 KB |
Execution killed with signal 9 |
3 |
Runtime error |
469 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Runtime error |
445 ms |
1048580 KB |
Execution killed with signal 9 |
5 |
Runtime error |
418 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Runtime error |
423 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Correct |
19 ms |
30292 KB |
Output is correct |
8 |
Correct |
24 ms |
34080 KB |
Output is correct |
9 |
Correct |
29 ms |
40112 KB |
Output is correct |
10 |
Correct |
16 ms |
22112 KB |
Output is correct |
11 |
Correct |
23 ms |
32328 KB |
Output is correct |
12 |
Correct |
9 ms |
15572 KB |
Output is correct |
13 |
Runtime error |
474 ms |
1048576 KB |
Execution killed with signal 9 |
14 |
Correct |
427 ms |
611532 KB |
Output is correct |
15 |
Correct |
446 ms |
677348 KB |
Output is correct |
16 |
Correct |
334 ms |
434624 KB |
Output is correct |
17 |
Runtime error |
421 ms |
1048576 KB |
Execution killed with signal 9 |
18 |
Runtime error |
427 ms |
1048576 KB |
Execution killed with signal 9 |
19 |
Runtime error |
447 ms |
1048576 KB |
Execution killed with signal 9 |
20 |
Runtime error |
437 ms |
1048576 KB |
Execution killed with signal 9 |
21 |
Runtime error |
421 ms |
1048576 KB |
Execution killed with signal 9 |
22 |
Runtime error |
434 ms |
1048576 KB |
Execution killed with signal 9 |
23 |
Runtime error |
420 ms |
1048576 KB |
Execution killed with signal 9 |
24 |
Runtime error |
421 ms |
1048576 KB |
Execution killed with signal 9 |
25 |
Runtime error |
423 ms |
1048576 KB |
Execution killed with signal 9 |
26 |
Runtime error |
419 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
443 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
434 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
420 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
407 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Runtime error |
423 ms |
1048576 KB |
Execution killed with signal 9 |
32 |
Runtime error |
406 ms |
1048576 KB |
Execution killed with signal 9 |