#include <bits/stdc++.h>
using namespace std;
#define MAXN 4001
int h,w;
int a[MAXN][MAXN];
int dist[MAXN][MAXN];
deque<pair<int,int>> bfsq;
pair<int,int> dir[4];
int ans=0;
bool check(int nodex,int nodey)
{
if (nodex>=1 and nodex<=h and nodey>=1 and nodey<=w and a[nodex][nodey]!='.' and dist[nodex][nodey]==INT_MAX) return true;
return false;
}
void bfs()
{
for (int i=1;i<=h;i++)
{
for (int j=1;j<=w;j++) dist[i][j]=INT_MAX;
}
dist[1][1]=0;bfsq.push_front({1,1});
while (bfsq.empty()==false)
{
int nodex=bfsq.front().first,nodey=bfsq.front().second;bfsq.pop_front();
ans=max(ans,dist[nodex][nodey]);
for (int i=0;i<4;i++)
{
int sledx=nodex+dir[i].first,sledy=nodey+dir[i].second;
if (!check(sledx,sledy)) continue;
if (a[sledx][sledy]==a[nodex][nodey]) {dist[sledx][sledy]=dist[nodex][nodey];bfsq.push_front({sledx,sledy});}
else {dist[sledx][sledy]=dist[nodex][nodey]+1;bfsq.push_back({sledx,sledy});}
}
}
}
int main()
{
dir[0]={0,1},dir[1]={1,0},dir[2]={0,-1},dir[3]={-1,0};
cin>>h>>w;
for (int i=1;i<=h;i++)
{
string s;cin>>s;
for (int j=1;j<=w;j++) a[i][j]=s[j-1];
}
bfs();
cout<<ans+1<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19036 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
9 ms |
19292 KB |
Output is correct |
5 |
Correct |
4 ms |
12888 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
5 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
14 |
Correct |
4 ms |
12816 KB |
Output is correct |
15 |
Correct |
13 ms |
19256 KB |
Output is correct |
16 |
Correct |
14 ms |
19272 KB |
Output is correct |
17 |
Correct |
11 ms |
19036 KB |
Output is correct |
18 |
Correct |
8 ms |
19292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
45404 KB |
Output is correct |
2 |
Correct |
51 ms |
33472 KB |
Output is correct |
3 |
Correct |
378 ms |
141248 KB |
Output is correct |
4 |
Correct |
100 ms |
54096 KB |
Output is correct |
5 |
Correct |
204 ms |
104016 KB |
Output is correct |
6 |
Correct |
809 ms |
155580 KB |
Output is correct |
7 |
Correct |
13 ms |
46684 KB |
Output is correct |
8 |
Correct |
13 ms |
45404 KB |
Output is correct |
9 |
Correct |
2 ms |
4792 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
12 ms |
46172 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
47 ms |
33464 KB |
Output is correct |
14 |
Correct |
32 ms |
26052 KB |
Output is correct |
15 |
Correct |
26 ms |
28552 KB |
Output is correct |
16 |
Correct |
23 ms |
15560 KB |
Output is correct |
17 |
Correct |
146 ms |
58028 KB |
Output is correct |
18 |
Correct |
99 ms |
56748 KB |
Output is correct |
19 |
Correct |
110 ms |
53844 KB |
Output is correct |
20 |
Correct |
83 ms |
50624 KB |
Output is correct |
21 |
Correct |
218 ms |
107344 KB |
Output is correct |
22 |
Correct |
216 ms |
103964 KB |
Output is correct |
23 |
Correct |
252 ms |
87032 KB |
Output is correct |
24 |
Correct |
209 ms |
106576 KB |
Output is correct |
25 |
Correct |
400 ms |
141372 KB |
Output is correct |
26 |
Correct |
644 ms |
172896 KB |
Output is correct |
27 |
Correct |
692 ms |
166912 KB |
Output is correct |
28 |
Correct |
801 ms |
155580 KB |
Output is correct |
29 |
Correct |
825 ms |
152992 KB |
Output is correct |
30 |
Correct |
768 ms |
157896 KB |
Output is correct |
31 |
Correct |
551 ms |
111864 KB |
Output is correct |
32 |
Correct |
747 ms |
165344 KB |
Output is correct |