Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<queue>
- using namespace std;
- const int di[]={1,0,-1,0};
- const int dj[]={0,1,0,-1};
- int n, m;
- queue<pair<int,int>>coada;
- int a[1001][1001],viz[1001][1001];
- bool inside(int i,int j)
- {
- return i>=1&&j>=1&&i<=n&&j<=m;
- }
- void lee()
- {
- while(!coada.empty())
- {
- int iv=coada.front().first;
- int jv=coada.front().second;
- coada.pop();
- for(int k=0;k<4;++k)
- {
- int i=iv+di[k],j=jv+dj[k];
- if (inside(i,j)&&!viz[i][j])
- viz[i][j]=viz[iv][jv]+1,coada.push(make_pair(i,j));
- }
- }
- }
- int main()
- {
- cin>>n>>m;
- for(int i=1;i<=n;++i)
- for(int j=1;j<=m;++j)
- cin>>a[i][j];
- for(int i=1;i<=n;++i)
- for(int j=1;j<=m;++j)
- if(!viz[i][j])
- for (int k=0;k<4;++k)
- {
- int iv=i+di[k],jv=j+dj[k];
- if(inside(iv,jv)&&a[iv][jv]!=a[i][j])
- viz[i][j]=1,coada.push(make_pair(i,j));
- }
- lee();
- for(int i=1;i<=n;++i,cout<<'\n')
- for(int j=1;j<=m;++j)
- cout<<viz[i][j]-1 <<' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement