Ganesh1648

SPIDEY SENSE

Jun 10th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define pb push_back
  6. #define vi vector<int>
  7. #define vpii vector<pair<int, int>>
  8. #define N 2e5 + 5
  9. #define f first
  10. #define s second
  11. #define all(x) x.begin(), x.end()
  12. #define forn(i, n) for (int i = 0; i < n; i++)
  13. #define fore(i, l, r) for (int i = l; i < r; i++)
  14. #define sz(a) (int)((a).size())
  15. #define ll long long
  16. #define ar array
  17. #define init(arr) memset(arr, 0, sizeof(arr))
  18. #define endl "\n"
  19. #define mp make_pair
  20.  
  21. int m,n;
  22. char mat[50][50];
  23. int x[]={1,-1,0,0};
  24. int y[]={0,0,1,-1};
  25. bool vis[50][50];
  26.  
  27. bool isValid(int r,int c)
  28. {
  29. return (r>=0&&c>=0&&r<m&&c<n);
  30. }
  31. int dfs(int r,int c)
  32. {
  33. vis[r][c]=true;
  34. if(mat[r][c]=='B')
  35. return 0;
  36. else if(mat[r][c]=='W')
  37. return INT_MAX;
  38.  
  39. int ans=INT_MAX;
  40. for(int i=0;i<4;i++)
  41. {
  42. int rr=r+x[i];
  43. int cc=c+y[i];
  44. if(isValid(rr,cc)&&(!vis[rr][cc]))
  45. {
  46. int temp=dfs(rr,cc);
  47. if(temp!=INT_MAX)
  48. ans=min(ans,1+temp);
  49. }
  50. }
  51. return ans;
  52. }
  53. int main()
  54. {
  55. ios_base::sync_with_stdio(false);
  56. cin.tie(NULL);
  57. cout.tie(NULL);
  58. int t;
  59. cin>>t;
  60. while (t-- > 0) {
  61. cin>>m>>n;
  62. for(int i=0;i<m;i++)
  63. {
  64. for(int j=0;j<n;j++)
  65. {
  66. cin>>mat[i][j];
  67. }
  68. }
  69. int res[m][n];
  70. for(int i=0;i<m;i++)
  71. {
  72. for(int j=0;j<n;j++)
  73. {
  74. if(mat[i][j]=='O')
  75. {
  76. memset(vis,false,sizeof(vis));
  77. int ans=dfs(i,j);
  78. if(ans==INT_MAX)
  79. res[i][j]=-1;
  80. else
  81. res[i][j]=ans;
  82. }
  83. }
  84. }
  85. for(int i=0;i<m;i++)
  86. {
  87. for(int j=0;j<n;j++)
  88. {
  89. if(mat[i][j]=='B')
  90. res[i][j]=0;
  91. else if(mat[i][j]=='W')
  92. res[i][j]=-1;
  93.  
  94. cout<<res[i][j]<<" ";
  95. }
  96. cout<<endl;
  97. }
  98. }
  99. }
Add Comment
Please, Sign In to add comment