Advertisement
a53

emigrare

a53
Mar 21st, 2022
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include<iostream>
  2. #include<queue>
  3. using namespace std;
  4. const int di[]={1,0,-1,0};
  5. const int dj[]={0,1,0,-1};
  6. int n, m;
  7. queue<pair<int,int>>coada;
  8. int a[1001][1001],viz[1001][1001];
  9.  
  10. bool inside(int i,int j)
  11. {
  12. return i>=1&&j>=1&&i<=n&&j<=m;
  13. }
  14.  
  15. void lee()
  16. {
  17. while(!coada.empty())
  18. {
  19. int iv=coada.front().first;
  20. int jv=coada.front().second;
  21. coada.pop();
  22. for(int k=0;k<4;++k)
  23. {
  24. int i=iv+di[k],j=jv+dj[k];
  25. if (inside(i,j)&&!viz[i][j])
  26. viz[i][j]=viz[iv][jv]+1,coada.push(make_pair(i,j));
  27. }
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. cin>>n>>m;
  34. for(int i=1;i<=n;++i)
  35. for(int j=1;j<=m;++j)
  36. cin>>a[i][j];
  37. for(int i=1;i<=n;++i)
  38. for(int j=1;j<=m;++j)
  39. if(!viz[i][j])
  40. for (int k=0;k<4;++k)
  41. {
  42. int iv=i+di[k],jv=j+dj[k];
  43. if(inside(iv,jv)&&a[iv][jv]!=a[i][j])
  44. viz[i][j]=1,coada.push(make_pair(i,j));
  45. }
  46. lee();
  47. for(int i=1;i<=n;++i,cout<<'\n')
  48. for(int j=1;j<=m;++j)
  49. cout<<viz[i][j]-1 <<' ';
  50. return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement