fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[200][200],n,m;
  4. void f(int x,int y,int val)
  5. { if(x<0 || x>=n || y<0 || y>=m)
  6. return;
  7. if(a[x][y]==-1 || (a[x][y]>0 && a[x][y]<=val))
  8. return;
  9. a[x][y]=val;
  10. f(x-1,y,val+1);
  11. f(x+1,y,val+1);
  12. f(x,y-1,val+1);
  13. f(x,y+1,val+1);
  14. return;
  15. }
  16. int main()
  17. { int t;
  18. scanf("%d",&t);
  19. while(t--)
  20. { int i,j;
  21. char s[200];
  22. scanf("%d%d",&n,&m);
  23.  
  24. for(i=0;i<n;i++)
  25. { scanf("%s",s);
  26. for(j=0;j<m;j++)
  27. if(s[j]=='0')
  28. a[i][j]=0;
  29. else
  30. a[i][j]=-1;
  31. }
  32.  
  33. for(i=0;i<n;i++)
  34. for(j=0;j<m;j++)
  35. if(a[i][j]==-1)
  36. { f(i-1,j,1);
  37. f(i+1,j,1);
  38. f(i,j-1,1);
  39. f(i,j+1,1);
  40. }
  41.  
  42. for(i=0;i<n;i++)
  43. { for(j=0;j<m;j++)
  44. if(a[i][j]!=-1)
  45. printf("%d ",a[i][j]);
  46. else
  47. printf("0 ");
  48. printf("\n");
  49. }
  50. }
  51. return 0;
  52. }
Success #stdin #stdout 0s 3620KB
stdin
1
3 4
0001
0011
0110
stdout
3 2 1 0 
2 1 0 0 
1 0 0 1