#include <bits/stdc++.h>
using namespace std;
int w, h, ans;
char snow[4040][4040];
int check[4040][4040];
queue<pair< pair<int, int>, int> >ord;
int brr=1;
void dfs(int x, int y, int br)
{
//cout<<br<<"\n";
if(x-1>0)
{
if(snow[x-1][y]==snow[x][y] and check[x-1][y]==0){check[x-1][y]=br; dfs(x-1, y, br);}
else if(snow[x-1][y]!='.')
{
if(check[x-1][y]==0){ord.push({{x-1, y}, br+1});}
}
else if(snow[x-1][y]=='.')check[x-1][y]=-1;
}
if(x+1<=h)
{
if(snow[x][y]==snow[x+1][y] and check[x+1][y]==0){check[x+1][y]=br; dfs(x+1, y, br);}
else if(snow[x+1][y]!='.')
{
if(check[x+1][y]==0){ord.push({{x+1, y}, br+1});}
}
else if(snow[x+1][y]=='.')check[x+1][y]=-1;
}
if(y-1>0)
{
if(snow[x][y]==snow[x][y-1] and check[x][y-1]==0){check[x][y-1]=br; dfs(x, y-1, br);}
else if(snow[x][y-1]!='.')
{
if(check[x][y-1]==0){ord.push({{x, y-1}, br+1});}
}
else if(snow[x][y-1]=='.')check[x][y-1]=-1;
}
if(y+1<=w)
{
if(snow[x][y]==snow[x][y+1] and check[x][y+1]==0){check[x][y+1]=br; dfs(x, y+1, br);}
else if(snow[x][y+1]!='.')
{
if(check[x][y+1]==0){ord.push({{x, y+1}, br+1});}
}
else if(snow[x][y+1]=='.')check[x][y+1]=-1;
}
return;
}
int main()
{
cin>>h>>w;
for(int i=1; i<=h; i++)
{
for(int j=1; j<=w; j++)cin>>snow[i][j];
}
ord.push({{1, 1}, 1});
while(!ord.empty())
{
int xx=ord.front().first.first, yy=ord.front().first.second, bb=ord.front().second;
ord.pop();
if(!check[xx][yy])
{
check[xx][yy]=bb;
dfs(xx, yy, bb);
ans=max(ans, bb);
}
}
//for(int i=1; i<=h; i++){for(int j=1; j<=w; j++)cout<<check[i][j]; cout<<endl;}
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
7184 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2492 KB |
Output is correct |
4 |
Correct |
17 ms |
7512 KB |
Output is correct |
5 |
Correct |
5 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
4 ms |
3672 KB |
Output is correct |
11 |
Correct |
4 ms |
4444 KB |
Output is correct |
12 |
Correct |
8 ms |
4040 KB |
Output is correct |
13 |
Correct |
5 ms |
4036 KB |
Output is correct |
14 |
Correct |
4 ms |
3932 KB |
Output is correct |
15 |
Correct |
17 ms |
7864 KB |
Output is correct |
16 |
Correct |
18 ms |
7304 KB |
Output is correct |
17 |
Correct |
13 ms |
9640 KB |
Output is correct |
18 |
Correct |
11 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
31580 KB |
Output is correct |
2 |
Correct |
83 ms |
22100 KB |
Output is correct |
3 |
Correct |
622 ms |
79032 KB |
Output is correct |
4 |
Correct |
162 ms |
37664 KB |
Output is correct |
5 |
Correct |
383 ms |
65080 KB |
Output is correct |
6 |
Correct |
1051 ms |
219664 KB |
Output is correct |
7 |
Correct |
11 ms |
33152 KB |
Output is correct |
8 |
Correct |
13 ms |
31748 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
2500 KB |
Output is correct |
11 |
Correct |
11 ms |
32348 KB |
Output is correct |
12 |
Correct |
2 ms |
3164 KB |
Output is correct |
13 |
Correct |
75 ms |
22100 KB |
Output is correct |
14 |
Correct |
51 ms |
15184 KB |
Output is correct |
15 |
Correct |
40 ms |
13284 KB |
Output is correct |
16 |
Correct |
33 ms |
7772 KB |
Output is correct |
17 |
Correct |
188 ms |
36508 KB |
Output is correct |
18 |
Correct |
162 ms |
34644 KB |
Output is correct |
19 |
Correct |
153 ms |
37716 KB |
Output is correct |
20 |
Correct |
136 ms |
28248 KB |
Output is correct |
21 |
Correct |
361 ms |
58896 KB |
Output is correct |
22 |
Correct |
387 ms |
64860 KB |
Output is correct |
23 |
Correct |
365 ms |
52656 KB |
Output is correct |
24 |
Correct |
342 ms |
64336 KB |
Output is correct |
25 |
Correct |
730 ms |
88360 KB |
Output is correct |
26 |
Runtime error |
1164 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Correct |
1352 ms |
821604 KB |
Output is correct |
28 |
Correct |
1030 ms |
211372 KB |
Output is correct |
29 |
Correct |
1008 ms |
204628 KB |
Output is correct |
30 |
Correct |
1139 ms |
392064 KB |
Output is correct |
31 |
Correct |
699 ms |
67500 KB |
Output is correct |
32 |
Correct |
1365 ms |
726692 KB |
Output is correct |