fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,arr[40][40];
  4. struct node{
  5. int a,b;
  6. };
  7. queue<node> q;
  8. void bfs(int x,int y){
  9. q.push({x,y});
  10. while(!q.empty()){
  11. x=q.front().a,y=q.front().b;
  12. q.pop();
  13. if(x>n+1 || x<0 || y>n+1 || y<0){
  14. continue;
  15. }
  16. if(arr[x][y]==1 || arr[x][y]==2){
  17. continue;
  18. }
  19. arr[x][y]=2;
  20. q.push({x-1,y});
  21. q.push({x+1,y});
  22. q.push({x,y+1});
  23. q.push({x,y-1});
  24. }
  25. }
  26. int main() {
  27. cin>>n;
  28. for(int i=0;i<=n+1;i++){
  29. for(int j=0;j<=n+1;j++){
  30. if(i==0 || i==n+1 || j==0 || j==n+1){
  31. arr[i][j]=0;
  32. }
  33. }
  34. }
  35. for(int i=1;i<=n;i++){
  36. for(int j=1;j<=n;j++){
  37. cin>>arr[i][j];
  38. }
  39. }
  40. bfs(0,0);
  41. for(int i=1;i<=n;i++){
  42. for(int j=1;j<=n;j++){
  43. if(arr[i][j]==2){
  44. cout<<0<<" ";
  45. }else if(arr[i][j]==0){
  46. cout<<2<<" ";
  47. }else{
  48. cout<<1<<" ";
  49. }
  50. }
  51. cout<<"\n";
  52. }
  53. }
Success #stdin #stdout 0s 5320KB
stdin
6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
stdout
0 0 0 0 0 0 
0 0 1 1 1 1 
0 1 1 2 2 1 
1 1 2 2 2 1 
1 2 2 2 2 1 
1 1 1 1 1 1