Saleh127

UVA 11561 / Flood Fill

Jun 19th, 2022
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. /***
  2.  created: 2022-06-20-02.38.10
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  13. #define get_lost_idiot return 0
  14. #define nl '\n'
  15.  
  16. ll n,m,ans=0;
  17. char a[105][105];
  18. ll v[105][105];
  19.  
  20. bool valid(ll i,ll j)
  21. {
  22.     return (a[i][j]=='T');
  23. }
  24.  
  25. void dfs(ll i,ll j)
  26. {
  27.    if(v[i][j] || a[i][j]=='#' || a[i][j]=='T') return;
  28.  
  29.    v[i][j]=1;
  30.  
  31.    if(a[i][j]=='G') ans++;
  32.  
  33.    if(valid(i,j+1)) return;
  34.    if(valid(i,j-1)) return;
  35.    if(valid(i+1,j)) return;
  36.    if(valid(i-1,j)) return;
  37.  
  38.    dfs(i,j+1);
  39.    dfs(i,j-1);
  40.    dfs(i+1,j);
  41.    dfs(i-1,j);
  42. }
  43.  
  44.  
  45. int main()
  46. {
  47.     ios_base::sync_with_stdio(0);
  48.     cin.tie(0);
  49.     cout.tie(0);
  50.  
  51.     ll i,j,k,l;
  52.  
  53.     while(cin>>m>>n && n && m)
  54.     {
  55.         for(i=0;i<n;i++)
  56.         {
  57.             for(j=0;j<m;j++)
  58.             {
  59.                 cin>>a[i][j];
  60.                 v[i][j]=0;
  61.             }
  62.         }
  63.  
  64.         for(i=0;i<n;i++)
  65.         {
  66.             for(j=0;j<m;j++)
  67.             {
  68.                 if(a[i][j]=='P')
  69.                 {
  70.                     dfs(i,j);
  71.                     break;
  72.                 }
  73.             }
  74.         }
  75.  
  76.         cout<<ans<<nl;
  77.  
  78.         ans=0;
  79.     }
  80.  
  81.     get_lost_idiot;
  82. }
  83.  
  84. /*
  85.  
  86. 7 4
  87. #######
  88. #P.GTG#
  89. #..TGG#
  90. #######
  91. 8 6
  92. ########
  93. #...GTG#
  94. #..PG.G#
  95. #...G#G#
  96. #..TG.G#
  97. ########
  98.  
  99. */
  100.  
Add Comment
Please, Sign In to add comment