Advertisement
Saleh127

UVA 722

Jun 22nd, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define ll long long
  5. string a[200],b;
  6. bool v[200][200];
  7. ll n,m,ans=0;
  8.  
  9. bool check(ll i,ll j)
  10. {
  11. if(i<0 || j<0 || i>=n || j>=m || v[i][j]==1 || a[i][j]=='1') return 0;
  12. return 1;
  13. }
  14.  
  15. void dfs(ll i,ll j)
  16. {
  17. v[i][j]=1;
  18. ans++;
  19.  
  20. if(check(i,j+1)) dfs(i,j+1);
  21. if(check(i,j-1)) dfs(i,j-1);
  22. if(check(i+1,j)) dfs(i+1,j);
  23. if(check(i-1,j)) dfs(i-1,j);
  24.  
  25. }
  26. int main()
  27. {
  28. ios_base::sync_with_stdio(0);
  29. cin.tie(0);
  30. cout.tie(0);
  31.  
  32.  
  33.  
  34. int t;
  35. cin>>t;
  36.  
  37. getline(cin,b);
  38. getline(cin,b);
  39.  
  40. for(int cs=1; cs<=t; cs++)
  41. {
  42.  
  43. ll i,j,k,l;
  44.  
  45. n=m=0;
  46.  
  47. cin>>i>>j;
  48.  
  49. getline(cin,b);
  50.  
  51. while(getline(cin,b))
  52. {
  53. if(b.size()==0) break;
  54. a[n++]=b;
  55. }
  56.  
  57. m=a[0].size();
  58.  
  59. dfs(i-1,j-1);
  60.  
  61. cout<<ans<<endl;
  62.  
  63. if(cs<t) cout<<endl;
  64.  
  65. memset(v,0,sizeof v);
  66. ans=0;
  67.  
  68. }
  69.  
  70. return 0;
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement