fork(1) download
  1. #include<iostream>
  2. #include<cstring>
  3. #include<utility>
  4. using namespace std;
  5. #define loop(i,n) for(int i=0;i<n;i++)
  6. int abs(int x)
  7. {
  8. if(x<0)
  9. return (-x);
  10. else
  11. return x;
  12. }
  13. int main()
  14. {
  15. int t,n,m;
  16. char s[200][200];
  17. pair<int,int> zero[40000],one[40000];
  18. cin>>t;
  19. while(t--)
  20. {
  21. cin>>n>>m;
  22. int k=0,l=0;
  23. loop(i,n) cin>>s[i];
  24. loop(i,n) {
  25. loop(j,m) {
  26. if(s[i][j]=='0')
  27. {
  28. zero[k].first=i;
  29. zero[k].second=j;
  30. k++;
  31. }
  32. else
  33. {
  34. one[l].first=i;
  35. one[l].second=j;
  36. l++;
  37. s[i][j]='0';
  38. }
  39. }
  40. }
  41. int min,diff;
  42. loop(i,k) {
  43. min=999999;
  44. loop(j,l) {
  45. diff=(abs(zero[i].first-one[j].first)+abs(zero[i].second-one[j].second));
  46. if(min>diff)
  47. min=diff;
  48. }
  49. s[zero[i].first][zero[i].second]=min+'0';
  50. }
  51. loop(i,n) {
  52. loop(j,m) {
  53. cout<<s[i][j]<<" ";
  54. }
  55. cout<<endl;
  56. }
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0s 3688KB
stdin
1
3 4
0001
0011
0110
stdout
3 2 1 0 
2 1 0 0 
1 0 0 1