Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- using namespace std;
- #define pb push_back
- #define vi vector<int>
- #define vpii vector<pair<int, int>>
- #define N 2e5 + 5
- #define f first
- #define s second
- #define all(x) x.begin(), x.end()
- #define forn(i, n) for (int i = 0; i < n; i++)
- #define fore(i, l, r) for (int i = l; i < r; i++)
- #define sz(a) (int)((a).size())
- #define ll long long
- #define ar array
- #define init(arr) memset(arr, 0, sizeof(arr))
- #define endl "\n"
- #define mp make_pair
- int m,n;
- char mat[50][50];
- int x[]={1,-1,0,0};
- int y[]={0,0,1,-1};
- bool vis[50][50];
- bool isValid(int r,int c)
- {
- return (r>=0&&c>=0&&r<m&&c<n);
- }
- int dfs(int r,int c)
- {
- vis[r][c]=true;
- if(mat[r][c]=='B')
- return 0;
- else if(mat[r][c]=='W')
- return INT_MAX;
- int ans=INT_MAX;
- for(int i=0;i<4;i++)
- {
- int rr=r+x[i];
- int cc=c+y[i];
- if(isValid(rr,cc)&&(!vis[rr][cc]))
- {
- int temp=dfs(rr,cc);
- if(temp!=INT_MAX)
- ans=min(ans,1+temp);
- }
- }
- return ans;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- int t;
- cin>>t;
- while (t-- > 0) {
- cin>>m>>n;
- for(int i=0;i<m;i++)
- {
- for(int j=0;j<n;j++)
- {
- cin>>mat[i][j];
- }
- }
- int res[m][n];
- for(int i=0;i<m;i++)
- {
- for(int j=0;j<n;j++)
- {
- if(mat[i][j]=='O')
- {
- memset(vis,false,sizeof(vis));
- int ans=dfs(i,j);
- if(ans==INT_MAX)
- res[i][j]=-1;
- else
- res[i][j]=ans;
- }
- }
- }
- for(int i=0;i<m;i++)
- {
- for(int j=0;j<n;j++)
- {
- if(mat[i][j]=='B')
- res[i][j]=0;
- else if(mat[i][j]=='W')
- res[i][j]=-1;
- cout<<res[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- }
Add Comment
Please, Sign In to add comment