This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int x;
int y;
};
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;
void bfs(queue<node> q1,queue<node> q2,int broj)
{
if(q1.empty())
return;
brojac=broj;
while(!q1.empty())
{
node g=q1.front();
q1.pop();
for(int i=0;i<4;i++)
{
if(g.x+dx[i]<n&&g.x+dx[i]>=0&&g.y+dy[i]<m&&g.y+dy[i]>=0)
{
if(!vis[g.x+dx[i]][g.y+dy[i]]&&mat[g.x+dx[i]][g.y+dy[i]]!='.')
{
if(mat[g.x+dx[i]][g.y+dy[i]]!=mat[g.x][g.y])
{
node g1;
g1.x=g.x+dx[i];
g1.y=g.y+dy[i];
q2.push(g1);
vis[g1.x][g1.y]=true;
}
else
{
node g1;
g1.x=g.x+dx[i];
g1.y=g.y+dy[i];
q1.push(g1);
vis[g1.x][g1.y]=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++)
{
for(int j=0;j<m;j++)
cin>>mat[i][j];
}
queue<node> q1,q2;
node g;
g.x=0;
g.y=0;
q1.push(g);
vis[0][0]=true;
bfs(q1,q2,1);
cout<<brojac<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |