#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};
class cmp
{
public:
bool operator()(pair<int,int> a,pair<int,int> b)
{
return bfs[a.first][a.second]>bfs[b.first][b.second];
}
};
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<pair<int,int>,vector<pair<int,int>>,cmp>pq;
pq.push({1,1});
inqueue[1][1]=1;
while(!pq.empty())
{
pair<int,int>nou=pq.top();
int l=nou.first;
int c=nou.second;
pq.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])
{
pq.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 |
42 ms |
16836 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
26 ms |
16972 KB |
Output is correct |
5 |
Correct |
6 ms |
9560 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
6 ms |
9412 KB |
Output is correct |
11 |
Correct |
7 ms |
9308 KB |
Output is correct |
12 |
Correct |
13 ms |
9884 KB |
Output is correct |
13 |
Correct |
6 ms |
9564 KB |
Output is correct |
14 |
Correct |
6 ms |
9564 KB |
Output is correct |
15 |
Correct |
36 ms |
16820 KB |
Output is correct |
16 |
Correct |
44 ms |
16732 KB |
Output is correct |
17 |
Correct |
22 ms |
16476 KB |
Output is correct |
18 |
Correct |
26 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
58204 KB |
Output is correct |
2 |
Correct |
126 ms |
28496 KB |
Output is correct |
3 |
Correct |
676 ms |
94544 KB |
Output is correct |
4 |
Correct |
210 ms |
42580 KB |
Output is correct |
5 |
Correct |
377 ms |
72532 KB |
Output is correct |
6 |
Execution timed out |
2037 ms |
111016 KB |
Time limit exceeded |
7 |
Correct |
17 ms |
59484 KB |
Output is correct |
8 |
Correct |
17 ms |
58200 KB |
Output is correct |
9 |
Correct |
5 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
17 ms |
58880 KB |
Output is correct |
12 |
Correct |
2 ms |
7004 KB |
Output is correct |
13 |
Correct |
155 ms |
28472 KB |
Output is correct |
14 |
Correct |
69 ms |
24404 KB |
Output is correct |
15 |
Correct |
38 ms |
24016 KB |
Output is correct |
16 |
Correct |
55 ms |
12296 KB |
Output is correct |
17 |
Correct |
335 ms |
45264 KB |
Output is correct |
18 |
Correct |
163 ms |
45000 KB |
Output is correct |
19 |
Correct |
202 ms |
42064 KB |
Output is correct |
20 |
Correct |
172 ms |
40272 KB |
Output is correct |
21 |
Correct |
442 ms |
74836 KB |
Output is correct |
22 |
Correct |
384 ms |
72372 KB |
Output is correct |
23 |
Correct |
686 ms |
60756 KB |
Output is correct |
24 |
Correct |
387 ms |
74424 KB |
Output is correct |
25 |
Correct |
754 ms |
94720 KB |
Output is correct |
26 |
Execution timed out |
2033 ms |
149872 KB |
Time limit exceeded |
27 |
Execution timed out |
2033 ms |
127636 KB |
Time limit exceeded |
28 |
Execution timed out |
2081 ms |
111056 KB |
Time limit exceeded |
29 |
Execution timed out |
2041 ms |
111124 KB |
Time limit exceeded |
30 |
Execution timed out |
2032 ms |
126072 KB |
Time limit exceeded |
31 |
Correct |
1976 ms |
78128 KB |
Output is correct |
32 |
Execution timed out |
2081 ms |
127480 KB |
Time limit exceeded |