Advertisement
_no0B

Untitled

Nov 29th, 2021
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define N ((int)6e4 + 5)
  4. #define MOD ((int)998244353 + 0)
  5. #define MAX ((int)1e9 + 7)
  6. #define MAXL ((ll)1e18 + 7)
  7. #define MAXP ((int)1e3 + 7)
  8. #define thr 1e-8
  9. #define pi acos(-1)  /// pi = acos ( -1 )
  10. #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL)
  11. #define endl "\n"
  12.  
  13. using namespace std;
  14.  
  15.  
  16. string str[102];
  17.  
  18. int dxx[] = {-1,0,1,0}; /// dxx[3] , dyy[3] -> left
  19. int dyy[] = {0,1,0,-1};
  20.  
  21.  
  22.  
  23. int dis[100][100];
  24.  
  25. bool IsValid(int row , int col)
  26. {
  27.     if(min(row , col) < 0 || row >= n || col >= m || str[row][col] == '#' || str[row][col] == 'm') return 0;
  28. }
  29.  
  30. void bfs(int row , int col){
  31.     memset(dis,-,sizeof dis);
  32.     queue < pair < int , int > > que;
  33.     que.push({row , col});
  34.    // vis[row][col] = 1;
  35.     dis[row][col] = 0;
  36.     while(!que.empty()){
  37.         int row = que.front().first , col = que.front().second;
  38.         que.pop();
  39.         for(int i = 0 ; i < 4 ; i++){
  40.             int x = row + dxx[i] , y = col + dyy[i];
  41.             if(IsValid(x,y) && dis[x][y] == -){
  42.                 dis[x][y] = dis[row][col] + 1;
  43.                 que.push({x,y});
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49.  
  50.  
  51. int main()
  52. {
  53.  
  54.     /// problem: https://lightoj.com/problem/power-puff-girls
  55.     int t;
  56.     cin>>t;
  57.     while(t--){
  58.         int n , m;
  59.         cin>>n>>m;
  60.         for(int i = 0 ; i < n ; i++){
  61.             cin>>str[i];
  62.         }
  63.         for(int i = 0 ; i < n ; i++){
  64.             for(int j = 0 ; j < m ; j++){
  65.                 if(str[i][j] == 'h') bfs(i , j);
  66.             }
  67.         }
  68.         int ans = 0;
  69.         for(int i = 0 ; i < n ; i++){
  70.             for(int j = 0 ; j < m ; j++){
  71.                 if(str[i][j] >= 'a' && str[i][j] <= 'c') ans = max(ans , dis[i][j]);
  72.             }
  73.         }
  74.         cout<<ans<<endl;
  75.     }
  76.  
  77.     return 0;
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement