fork(1) download
  1. #include<queue>
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<cstring>
  6. #include<stdlib.h>
  7. #include<algorithm>
  8. #include<iomanip>
  9. #include<set>
  10. #include<vector>
  11. #include<utility>
  12. #include<stack>
  13. #define getcx getchar_unlocked
  14. #ifndef ONLINE_JUDGE
  15. #define getcx getchar
  16. #endif
  17. //#define mod 1000000007
  18. #define rep(i,n) for(int i=0;i<n;++i)
  19. #define rep1(i,j,n) for(int i=j;i<n;++i)
  20. #define repl1(i,j,n) for(long long i=j;i<n;++i)
  21. #define repl(i,n) for(long long i=0;i<n;++i)
  22. #define ll long long
  23. using namespace std;
  24. main()
  25. {
  26. int dx[4]={-1,1,0,0};
  27. int dy[4]={0,0,-1,1};
  28. ll t;
  29. queue<pair<int,int> > q;
  30. pair<int,int> temp;
  31. int n,m;
  32. int dist[185][185];
  33. bool visited[185][185];
  34. char a[185][185];
  35. scanf("%lld",&t);
  36. while(t--)
  37. {
  38. memset(visited,0,sizeof(visited));
  39. scanf("%d%d",&n,&m);
  40. for(int i=0;i<n;++i)
  41. scanf("%s",&a[i]);
  42. for(int i=0;i<n;++i)
  43. {
  44. // cout<<a[i]<<endl;
  45. for(int j=0;j<m;++j)
  46. {
  47. if(a[i][j]=='1')
  48. {
  49. temp=make_pair(i,j);
  50. q.push(temp);
  51. dist[i][j]=0;
  52. visited[i][j]=1;
  53. // cout<<i<<" "<<j<<endl;
  54. }
  55. }
  56. }
  57.  
  58. while(!q.empty())
  59. {
  60. temp=q.front();
  61. q.pop();
  62. //cout<<temp.first<<" "<<temp.second<<endl;
  63. //visited[temp.first][temp.second]=1;
  64. for(int i=0;i<4;++i)
  65. {
  66. int X=temp.first+dx[i];
  67. int Y=temp.second+dy[i];
  68. if(X>=0&&X<n&&Y>=0&&Y<m)
  69. {
  70. if(visited[X][Y]==0)
  71. {
  72. dist[X][Y]=dist[temp.first][temp.second]+1;
  73. visited[X][Y]=1;
  74. temp.first=X;temp.second=Y;
  75. q.push(temp);
  76. }
  77. }
  78. }
  79. /*for(int k=0;k<n;++k)
  80. {
  81. for(int j=0;j<m;++j)
  82. printf("%d ",dist[k][j]);
  83. printf("\n\n");
  84. }*/
  85. }
  86. for(int i=0;i<n;++i)
  87. {
  88. for(int j=0;j<m;++j)
  89. printf("%d ",dist[i][j]);
  90. printf("\n");
  91. }
  92. }
  93. }
  94.  
  95.  
Success #stdin #stdout 0s 3556KB
stdin
1
3 4
0001
0011
0110
stdout
3 2 1 0 
2 1 0 0 
1 0 0 1