#include<bits/stdc++.h>
using namespace std;
const int N=4e3+3;
char t[N][N];
int dp[N][N];
bool odw[N][N];
deque<pair<int,int>>Q;
int n,m;
bool ok(int a,int b)
{
if(a>n||a<1||b>m||b<1||t[a][b]=='.')return false;
return true;
}
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
cin>>t[i][j];
}
}
dp[1][1]=1;
Q.push_back({1,1});
int res=1;
while(!Q.empty())
{
int x=Q.front().first,y=Q.front().second;
Q.pop_front();
res=max(dp[x][y],res);
odw[x][y]=true;
if(!odw[x-1][y]&&ok(x-1,y))
{
odw[x-1][y]=true;
if(t[x-1][y]==t[x][y])
{
dp[x-1][y]=dp[x][y];
Q.push_front({x-1,y});
}
else
{
dp[x-1][y]=dp[x][y]+1;
Q.push_back({x-1,y});
}
}
if(!odw[x+1][y]&&ok(x+1,y))
{
odw[x+1][y]=true;
if(t[x+1][y]==t[x][y])
{
dp[x+1][y]=dp[x][y];
Q.push_front({x+1,y});
}
else
{
dp[x+1][y]=dp[x][y]+1;
Q.push_back({x+1,y});
}
}
if(!odw[x][y-1]&&ok(x,y-1))
{
odw[x][y-1]=true;
if(t[x][y-1]==t[x][y])
{
dp[x][y-1]=dp[x][y];
Q.push_front({x,y-1});
}
else
{
dp[x][y-1]=dp[x][y]+1;
Q.push_back({x,y-1});
}
}
if(!odw[x][y+1]&&ok(x,y+1))
{
odw[x][y+1]=true;
if(t[x][y+1]==t[x][y])
{
dp[x][y+1]=dp[x][y];
Q.push_front({x,y+1});
}
else
{
dp[x][y+1]=dp[x][y]+1;
Q.push_back({x,y+1});
}
}
}
cout<<res<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
7772 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
10 ms |
7516 KB |
Output is correct |
5 |
Correct |
5 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
4 ms |
3524 KB |
Output is correct |
11 |
Correct |
4 ms |
2908 KB |
Output is correct |
12 |
Correct |
8 ms |
4188 KB |
Output is correct |
13 |
Correct |
5 ms |
4188 KB |
Output is correct |
14 |
Correct |
5 ms |
3956 KB |
Output is correct |
15 |
Correct |
15 ms |
7184 KB |
Output is correct |
16 |
Correct |
15 ms |
7772 KB |
Output is correct |
17 |
Correct |
14 ms |
7260 KB |
Output is correct |
18 |
Correct |
10 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
46172 KB |
Output is correct |
2 |
Correct |
72 ms |
20052 KB |
Output is correct |
3 |
Correct |
596 ms |
88912 KB |
Output is correct |
4 |
Correct |
148 ms |
40268 KB |
Output is correct |
5 |
Correct |
378 ms |
65316 KB |
Output is correct |
6 |
Correct |
857 ms |
123068 KB |
Output is correct |
7 |
Correct |
19 ms |
48220 KB |
Output is correct |
8 |
Correct |
18 ms |
46172 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
20 ms |
46940 KB |
Output is correct |
12 |
Correct |
2 ms |
2136 KB |
Output is correct |
13 |
Correct |
73 ms |
19792 KB |
Output is correct |
14 |
Correct |
44 ms |
13908 KB |
Output is correct |
15 |
Correct |
41 ms |
17232 KB |
Output is correct |
16 |
Correct |
30 ms |
7764 KB |
Output is correct |
17 |
Correct |
179 ms |
36948 KB |
Output is correct |
18 |
Correct |
151 ms |
43860 KB |
Output is correct |
19 |
Correct |
152 ms |
40340 KB |
Output is correct |
20 |
Correct |
130 ms |
32596 KB |
Output is correct |
21 |
Correct |
341 ms |
64804 KB |
Output is correct |
22 |
Correct |
361 ms |
65360 KB |
Output is correct |
23 |
Correct |
337 ms |
53584 KB |
Output is correct |
24 |
Correct |
353 ms |
62288 KB |
Output is correct |
25 |
Correct |
633 ms |
109908 KB |
Output is correct |
26 |
Correct |
562 ms |
144928 KB |
Output is correct |
27 |
Correct |
754 ms |
127572 KB |
Output is correct |
28 |
Correct |
839 ms |
123068 KB |
Output is correct |
29 |
Correct |
838 ms |
123392 KB |
Output is correct |
30 |
Correct |
799 ms |
122584 KB |
Output is correct |
31 |
Correct |
572 ms |
86100 KB |
Output is correct |
32 |
Correct |
709 ms |
124644 KB |
Output is correct |