#include <bits/stdc++.h>
#define MAX 4005
using namespace std;
char mat[MAX][MAX];
int bfs[MAX][MAX];
bool inqueue[MAX][MAX];
int dl[]={-1,0,1,0};
int dc[]={0,1,0,-1};
int main()
{
int n,m;
cin>>n>>m;
int i,j;
for(i=1;i<=n;++i)
for(j=1;j<=m;++j)
{
cin>>mat[i][j];
bfs[i][j]=INT_MAX;
}
bfs[1][1]=1;
queue<pair<int,int>>q;
q.push({1,1});
inqueue[1][1]=1;
while(!q.empty())
{
pair<int,int>nou=q.front();
int l=nou.first;
int c=nou.second;
q.pop();
inqueue[l][c]=0;
for(i=0;i<4;++i)
{
int lnou=l+dl[i];
int cnou=c+dc[i];
int pret=bfs[l][c]+(mat[l][c]!=mat[lnou][cnou]);
if(isalpha(mat[lnou][cnou]) && pret<bfs[lnou][cnou])
{
bfs[lnou][cnou]=pret;
if(!inqueue[lnou][cnou])
{
q.push({lnou,cnou});
inqueue[lnou][cnou]=1;
}
}
}
}
int answer=-1;
for(i=1;i<=n;++i)
for(j=1;j<=m;++j)
if(isalpha(mat[i][j]))
answer=max(answer,bfs[i][j]);
cout<<answer;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
16700 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
21 ms |
16732 KB |
Output is correct |
5 |
Correct |
12 ms |
9564 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4724 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
6 ms |
9564 KB |
Output is correct |
11 |
Correct |
6 ms |
9436 KB |
Output is correct |
12 |
Correct |
33 ms |
9564 KB |
Output is correct |
13 |
Correct |
13 ms |
9788 KB |
Output is correct |
14 |
Correct |
11 ms |
9564 KB |
Output is correct |
15 |
Correct |
72 ms |
16640 KB |
Output is correct |
16 |
Correct |
141 ms |
16728 KB |
Output is correct |
17 |
Correct |
98 ms |
16468 KB |
Output is correct |
18 |
Correct |
21 ms |
16732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
58200 KB |
Output is correct |
2 |
Correct |
393 ms |
31180 KB |
Output is correct |
3 |
Execution timed out |
2078 ms |
94548 KB |
Time limit exceeded |
4 |
Correct |
1332 ms |
43344 KB |
Output is correct |
5 |
Correct |
352 ms |
72452 KB |
Output is correct |
6 |
Execution timed out |
2037 ms |
90836 KB |
Time limit exceeded |
7 |
Correct |
18 ms |
61016 KB |
Output is correct |
8 |
Correct |
21 ms |
61276 KB |
Output is correct |
9 |
Correct |
11 ms |
4608 KB |
Output is correct |
10 |
Correct |
2 ms |
4544 KB |
Output is correct |
11 |
Correct |
18 ms |
63440 KB |
Output is correct |
12 |
Correct |
2 ms |
7000 KB |
Output is correct |
13 |
Correct |
388 ms |
33876 KB |
Output is correct |
14 |
Correct |
199 ms |
24488 KB |
Output is correct |
15 |
Correct |
53 ms |
28756 KB |
Output is correct |
16 |
Correct |
339 ms |
12112 KB |
Output is correct |
17 |
Correct |
1399 ms |
46276 KB |
Output is correct |
18 |
Correct |
181 ms |
45908 KB |
Output is correct |
19 |
Correct |
1318 ms |
43712 KB |
Output is correct |
20 |
Execution timed out |
2025 ms |
41128 KB |
Time limit exceeded |
21 |
Execution timed out |
2037 ms |
74836 KB |
Time limit exceeded |
22 |
Correct |
379 ms |
72384 KB |
Output is correct |
23 |
Execution timed out |
2070 ms |
57784 KB |
Time limit exceeded |
24 |
Correct |
342 ms |
74520 KB |
Output is correct |
25 |
Correct |
739 ms |
94388 KB |
Output is correct |
26 |
Correct |
677 ms |
84052 KB |
Output is correct |
27 |
Correct |
980 ms |
94544 KB |
Output is correct |
28 |
Execution timed out |
2013 ms |
90756 KB |
Time limit exceeded |
29 |
Execution timed out |
2065 ms |
91728 KB |
Time limit exceeded |
30 |
Execution timed out |
2094 ms |
90452 KB |
Time limit exceeded |
31 |
Execution timed out |
2072 ms |
71752 KB |
Time limit exceeded |
32 |
Execution timed out |
2053 ms |
94804 KB |
Time limit exceeded |