fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define rep(i,a,b) for(ll i =a; i<b;++i)
  7. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  8. #define endl "\n"
  9. #define pb push_back
  10. #define F first
  11. #define S second
  12. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  13. #define print1(a) for(auto x : a) cout << x.F << " " << x.S << endl
  14. #define print2(a,x,y) for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
  15. #define mk(x,y) make_pair(x,y)
  16. typedef long long int ll;
  17. const ll N = 1e2+ 5 ;
  18. const ll mod = 1e9+7 ;
  19. ll n , m ,ans[N][N] ,vis[N][N];
  20. char a[15][15] ;
  21. ll dx[] = { -1 ,0 , 0, 1};
  22. ll dy[] = { 0 ,-1, 1, 0};
  23. ll pow_mod_m(ll a , ll n)
  24. {
  25. if(!n)
  26. return 1;
  27. ll pt = pow_mod_m(a,n/2);
  28. pt *= pt ;
  29. if(n&1)
  30. pt *= a;
  31. return pt;
  32. }
  33.  
  34. /*bool comp(const ll &p1, const ll &p2)
  35.   {
  36.   if(order[p1] == order[p2])
  37.   return p2 > p1;
  38.   return order[p1] > order[p2];
  39.   }*/
  40.  
  41.  
  42. int main()
  43. {
  44. IOS;
  45. int ts =1;
  46. cin >> ts;
  47. vector < pair <ll , ll> > v;
  48. while(ts --)
  49. {
  50. memset(vis , 0, sizeof(vis));
  51. cin >> n >> m;
  52. rep(i,1,n+1) rep(j,1,m+1) ans[i][j] = mod;
  53. rep(i,1,n+1)
  54. {
  55. rep(j,1,m+1) {cin >> a[i][j] ;
  56. if(a[i][j] =='B') ans[i][j] = 0,v.pb(mk(i,j));
  57. if(a[i][j] == 'W') ans[i][j] = -1;
  58. }
  59. }
  60. queue < pair <pair <ll,ll>,ll > > q;
  61. for(auto u : v) q.push(mk(u,0)) , vis[u.F][u.S] = 1;
  62.  
  63. while(q.empty() == false)
  64. {
  65. auto p = q.front();
  66. ///cout << p.F.F << " " << p.F.S << endl;
  67. q.pop();
  68. rep(i,0,4)
  69. {
  70. if(p.F.F + dx[i] >= 1 and p.F.S + dy[i] >=1 and p.F.F + dx[i] <= n and p.F.S + dy[i] <= m and !vis[p.F.F + dx[i]][p.F.S+dy[i]] and a[p.F.F+dx[i]][p.F.S+dy[i]] != 'W' )
  71. {
  72. auto z = mk(mk(p.F.F + dx[i] , p.F.S + dy[i] ) , p.S+1);
  73. ans[p.F.F+dx[i]][p.F.S+dy[i]] = p.S + 1;
  74. q.push(z);
  75. vis[p.F.F +dx[i]][p.F.S+dy[i]] = 1;
  76. }
  77. else
  78. {
  79. ans[p.F.F +dx[i]][p.F.S + dy[i]] = min(ans[p.F.F +dx[i]][p.F.S + dy[i]],p.S + 1);
  80. }
  81. }
  82. }
  83. rep(i,1,n+1)
  84. {
  85. rep(j,1,m+1)
  86. {
  87. if(ans[i][j] == mod) cout << -1 << " ";
  88. else cout << ans[i][j] << " ";
  89. }
  90. cout << endl;
  91. }
  92. v.clear();
  93. }
  94. }
  95.  
Success #stdin #stdout 0s 4332KB
stdin
Standard input is empty
stdout
Standard output is empty