#include <iostream>
#include <queue>
using namespace std;
int n,m;
char mat[4004][4004];
bool vis[4004][4004];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int brojac;
queue<pair<int,int> > q1,q2;
void bfs(queue<pair<int,int> > &q1,queue<pair<int,int> > &q2,int broj)
{
if(q1.empty())
return;
brojac=broj;
while(!q1.empty())
{
pair<int,int> g=q1.front();
q1.pop();
for(int i=0;i<4;i++)
{
if(g.first+dx[i]<n&&g.first+dx[i]>=0&&g.second+dy[i]<m&&g.second+dy[i]>=0)
{
if(!vis[g.first+dx[i]][g.second+dy[i]]&&mat[g.first+dx[i]][g.second+dy[i]]!='.')
{
if(mat[g.first+dx[i]][g.second+dy[i]]!=mat[g.first][g.second])
{
q2.push(make_pair(g.first+dx[i],g.second+dy[i]));
vis[g.first+dx[i]][g.second+dy[i]]=true;
}
else
{
q1.push(make_pair(g.first+dx[i],g.second+dy[i]));
vis[g.first+dx[i]][g.second+dy[i]]=true;
}
}
}
}
}
bfs(q2,q1,broj+1);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>mat[i];
}
q1.push(make_pair(0,0));
vis[0][0]=true;
bfs(q1,q2,1);
cout<<brojac<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
4472 KB |
Output is correct |
2 |
Correct |
2 ms |
4472 KB |
Output is correct |
3 |
Correct |
2 ms |
4472 KB |
Output is correct |
4 |
Correct |
12 ms |
4952 KB |
Output is correct |
5 |
Correct |
5 ms |
4952 KB |
Output is correct |
6 |
Correct |
2 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
4952 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
5 ms |
4952 KB |
Output is correct |
11 |
Correct |
4 ms |
4952 KB |
Output is correct |
12 |
Correct |
8 ms |
4952 KB |
Output is correct |
13 |
Correct |
5 ms |
4952 KB |
Output is correct |
14 |
Correct |
5 ms |
4952 KB |
Output is correct |
15 |
Correct |
15 ms |
5648 KB |
Output is correct |
16 |
Correct |
17 ms |
5896 KB |
Output is correct |
17 |
Correct |
12 ms |
6004 KB |
Output is correct |
18 |
Correct |
11 ms |
6368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
32888 KB |
Output is correct |
2 |
Correct |
57 ms |
32888 KB |
Output is correct |
3 |
Correct |
257 ms |
106408 KB |
Output is correct |
4 |
Correct |
67 ms |
106408 KB |
Output is correct |
5 |
Correct |
493 ms |
477800 KB |
Output is correct |
6 |
Correct |
979 ms |
477800 KB |
Output is correct |
7 |
Correct |
26 ms |
477800 KB |
Output is correct |
8 |
Correct |
25 ms |
477800 KB |
Output is correct |
9 |
Correct |
3 ms |
477800 KB |
Output is correct |
10 |
Correct |
2 ms |
477800 KB |
Output is correct |
11 |
Correct |
27 ms |
477800 KB |
Output is correct |
12 |
Correct |
4 ms |
477800 KB |
Output is correct |
13 |
Correct |
53 ms |
477800 KB |
Output is correct |
14 |
Correct |
34 ms |
477800 KB |
Output is correct |
15 |
Correct |
35 ms |
477800 KB |
Output is correct |
16 |
Correct |
26 ms |
477800 KB |
Output is correct |
17 |
Correct |
151 ms |
477800 KB |
Output is correct |
18 |
Correct |
129 ms |
477800 KB |
Output is correct |
19 |
Correct |
70 ms |
477800 KB |
Output is correct |
20 |
Correct |
68 ms |
477800 KB |
Output is correct |
21 |
Correct |
160 ms |
477800 KB |
Output is correct |
22 |
Correct |
504 ms |
530568 KB |
Output is correct |
23 |
Correct |
282 ms |
530568 KB |
Output is correct |
24 |
Correct |
251 ms |
530568 KB |
Output is correct |
25 |
Correct |
546 ms |
530568 KB |
Output is correct |
26 |
Correct |
559 ms |
530568 KB |
Output is correct |
27 |
Correct |
734 ms |
530568 KB |
Output is correct |
28 |
Correct |
961 ms |
530568 KB |
Output is correct |
29 |
Correct |
939 ms |
530568 KB |
Output is correct |
30 |
Correct |
793 ms |
530568 KB |
Output is correct |
31 |
Correct |
739 ms |
530568 KB |
Output is correct |
32 |
Correct |
622 ms |
530568 KB |
Output is correct |