Advertisement
Saleh127

UVA 352

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