#include <bits/stdc++.h>
#define MAX 4005
#define PII pair<int,int>
using namespace std;
char mat[MAX][MAX];
int bfs[MAX][MAX];
bool inq[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;
deque<PII>deq;
deq.push_front({1,1});
while(!deq.empty())
{
PII nou=deq.front();
int l=nou.first;
int c=nou.second;
deq.pop_front();
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(mat[l][c]==mat[lnou][cnou])
deq.push_front({lnou,cnou});
else
deq.push_back({lnou,cnou});
}
}
}
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 |
17 ms |
14684 KB |
Output is correct |
2 |
Correct |
1 ms |
4540 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
10 ms |
14936 KB |
Output is correct |
5 |
Correct |
4 ms |
8644 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
4 ms |
8540 KB |
Output is correct |
11 |
Correct |
4 ms |
8540 KB |
Output is correct |
12 |
Correct |
6 ms |
8540 KB |
Output is correct |
13 |
Correct |
4 ms |
8540 KB |
Output is correct |
14 |
Correct |
4 ms |
8540 KB |
Output is correct |
15 |
Correct |
16 ms |
14836 KB |
Output is correct |
16 |
Correct |
15 ms |
14680 KB |
Output is correct |
17 |
Correct |
14 ms |
14684 KB |
Output is correct |
18 |
Correct |
11 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
43640 KB |
Output is correct |
2 |
Correct |
69 ms |
23636 KB |
Output is correct |
3 |
Correct |
536 ms |
79956 KB |
Output is correct |
4 |
Correct |
148 ms |
35664 KB |
Output is correct |
5 |
Correct |
342 ms |
60500 KB |
Output is correct |
6 |
Correct |
921 ms |
94648 KB |
Output is correct |
7 |
Correct |
12 ms |
46428 KB |
Output is correct |
8 |
Correct |
11 ms |
45148 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
14 ms |
47548 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
71 ms |
23508 KB |
Output is correct |
14 |
Correct |
41 ms |
20828 KB |
Output is correct |
15 |
Correct |
38 ms |
24924 KB |
Output is correct |
16 |
Correct |
30 ms |
10684 KB |
Output is correct |
17 |
Correct |
202 ms |
39252 KB |
Output is correct |
18 |
Correct |
152 ms |
39108 KB |
Output is correct |
19 |
Correct |
154 ms |
36692 KB |
Output is correct |
20 |
Correct |
131 ms |
34516 KB |
Output is correct |
21 |
Correct |
361 ms |
62656 KB |
Output is correct |
22 |
Correct |
328 ms |
60500 KB |
Output is correct |
23 |
Correct |
337 ms |
50888 KB |
Output is correct |
24 |
Correct |
341 ms |
62404 KB |
Output is correct |
25 |
Correct |
630 ms |
79956 KB |
Output is correct |
26 |
Correct |
605 ms |
120672 KB |
Output is correct |
27 |
Correct |
845 ms |
109356 KB |
Output is correct |
28 |
Correct |
887 ms |
94640 KB |
Output is correct |
29 |
Correct |
905 ms |
90544 KB |
Output is correct |
30 |
Correct |
902 ms |
98520 KB |
Output is correct |
31 |
Correct |
574 ms |
64848 KB |
Output is correct |
32 |
Correct |
776 ms |
99432 KB |
Output is correct |