#include <bits/stdc++.h>
#define MAX 4005
using namespace std;
char mat[MAX][MAX];
int bfs[MAX][MAX];
int dl[]={-1,0,1,0};
int dc[]={0,1,0,-1};
struct str
{
int lin,col,val;
};
class cmp
{
public:
bool operator()(str a,str b)
{
return bfs[a.lin][a.col]>bfs[b.lin][b.col];
}
};
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;
priority_queue<str,vector<str>,cmp>pq;
pq.push({1,1,1});
while(!pq.empty())
{
str nou=pq.top();
int l=nou.lin;
int c=nou.col;
int v=nou.val;
pq.pop();
if(v>bfs[l][c])
continue;
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;
pq.push({lnou,cnou,pret});
}
}
}
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 |
38 ms |
12892 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
27 ms |
13340 KB |
Output is correct |
5 |
Correct |
8 ms |
10588 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 |
4444 KB |
Output is correct |
10 |
Correct |
5 ms |
6492 KB |
Output is correct |
11 |
Correct |
8 ms |
6748 KB |
Output is correct |
12 |
Correct |
14 ms |
10908 KB |
Output is correct |
13 |
Correct |
6 ms |
10588 KB |
Output is correct |
14 |
Correct |
8 ms |
10588 KB |
Output is correct |
15 |
Correct |
31 ms |
12892 KB |
Output is correct |
16 |
Correct |
44 ms |
12892 KB |
Output is correct |
17 |
Correct |
21 ms |
12632 KB |
Output is correct |
18 |
Correct |
27 ms |
13340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
45660 KB |
Output is correct |
2 |
Correct |
125 ms |
24860 KB |
Output is correct |
3 |
Correct |
731 ms |
78684 KB |
Output is correct |
4 |
Correct |
225 ms |
35152 KB |
Output is correct |
5 |
Correct |
359 ms |
60236 KB |
Output is correct |
6 |
Execution timed out |
2074 ms |
103592 KB |
Time limit exceeded |
7 |
Correct |
12 ms |
48220 KB |
Output is correct |
8 |
Correct |
11 ms |
47196 KB |
Output is correct |
9 |
Correct |
5 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
11 ms |
41820 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
125 ms |
22888 KB |
Output is correct |
14 |
Correct |
75 ms |
17232 KB |
Output is correct |
15 |
Correct |
42 ms |
19384 KB |
Output is correct |
16 |
Correct |
57 ms |
10844 KB |
Output is correct |
17 |
Correct |
348 ms |
36184 KB |
Output is correct |
18 |
Correct |
153 ms |
36436 KB |
Output is correct |
19 |
Correct |
197 ms |
35132 KB |
Output is correct |
20 |
Correct |
150 ms |
32336 KB |
Output is correct |
21 |
Correct |
420 ms |
62032 KB |
Output is correct |
22 |
Correct |
387 ms |
60004 KB |
Output is correct |
23 |
Correct |
653 ms |
50768 KB |
Output is correct |
24 |
Correct |
339 ms |
61784 KB |
Output is correct |
25 |
Correct |
674 ms |
78692 KB |
Output is correct |
26 |
Execution timed out |
2055 ms |
168460 KB |
Time limit exceeded |
27 |
Execution timed out |
2037 ms |
128132 KB |
Time limit exceeded |
28 |
Execution timed out |
2015 ms |
103584 KB |
Time limit exceeded |
29 |
Execution timed out |
2044 ms |
103428 KB |
Time limit exceeded |
30 |
Execution timed out |
2080 ms |
127692 KB |
Time limit exceeded |
31 |
Correct |
1938 ms |
65736 KB |
Output is correct |
32 |
Execution timed out |
2050 ms |
128148 KB |
Time limit exceeded |