Advertisement
Josif_tepe

Untitled

Nov 21st, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6. int main()
  7. {
  8. int a,b;
  9. cin>>a>>b;
  10. char mat[a][b];
  11. bool v[a][b];
  12. for(int i=0;i<a;i++)
  13. {
  14. for(int j=0;j<b;j++)
  15. {
  16. cin>>mat[i][j];
  17. v[i][j]=false;
  18. }
  19. }
  20. int k=0;
  21. for(int i=0;i<a;i++)
  22. {
  23. for(int j=0;j<b;j++)
  24. {
  25. if(mat[i][j]=='+' && v[i][j]!=true)
  26. {
  27. queue<int>Q;
  28. Q.push(i);
  29. Q.push(j);
  30. while(!Q.empty())
  31. {
  32. int ci=Q.front();
  33. Q.pop();
  34. int cj=Q.front();
  35. Q.pop();
  36. if(ci+1<a && mat[ci+1][cj]!='-' && v[ci+1][cj]==false)
  37. {
  38. Q.push(ci+1);
  39. Q.push(cj);
  40. v[ci+1][cj]=true;
  41. }
  42. if(ci-1>=0 && mat[ci-1][cj]!='-' && v[ci-1][cj]==false)
  43. {
  44. Q.push(ci-1);
  45. Q.push(cj);
  46. v[ci-1][cj]=true;
  47. }
  48. if(cj+1<b && mat[ci][cj+1]!='-' && v[ci][cj+1]==false)
  49. {
  50. Q.push(ci);
  51. Q.push(cj+1);
  52. v[ci][cj+1]=true;
  53. }
  54. if(cj-1>=0 && mat[ci][cj-1]!='-' && v[ci][cj-1]==false)
  55. {
  56. Q.push(ci);
  57. Q.push(cj-1);
  58. v[ci][cj-1]=true;
  59. }
  60. }
  61. k++;
  62. }
  63. }
  64. }
  65. cout<<k;
  66. return 0;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement