Naxocist

IslandSearch v1

Mar 30th, 2022
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define endll '\n'
  3. using namespace std;
  4.  
  5.  
  6. int arr[500][500], cnt, c[500][500];
  7.  
  8. void bfs(int i, int j){
  9.     queue<pair<int, int>> q;
  10.     int v[3] = {-1, 0, 1};
  11.  
  12.     q.push(make_pair(i, j));
  13.     while(!q.empty()){
  14.         pair<int, int> p = q.front();
  15.         int a = p.first, b = p.second;
  16.    
  17.         c[a][b] = 1;
  18.    
  19.         for(int i=0; i<3; ++i){
  20.             for(int j=0; j<3; ++j){
  21.                 int h = a + v[i], k = b + v[j];
  22.                 if(arr[h][k] == 1 && c[h][k] == 0){
  23.                     q.push(make_pair(h, k));
  24.                     c[h][k] = 1;
  25.                 }
  26.             }
  27.         }
  28.  
  29.         q.pop();
  30.     }
  31.  
  32. }
  33.  
  34. int main() {
  35.     ios_base::sync_with_stdio(0); cin.tie(0);
  36.     int n; cin >> n;
  37.  
  38.     for(int i=0; i<n; ++i){
  39.         for(int j=0; j<n; ++j) cin >> arr[i][j];
  40.     }
  41.  
  42.     for(int i=0; i<n; ++i){
  43.         for(int j=0; j<n; ++j){
  44.             if(arr[i][j] == 1 && c[i][j] != 1){
  45.                 bfs(i, j);
  46.                 cnt++;
  47.             }
  48.         }
  49.     }
  50.  
  51.     cout << cnt;
  52.     return 0;
  53. }
  54.  
  55. /*
  56. 5
  57. 0 0 0 0 0
  58. 0 1 1 1 0
  59. 0 0 0 0 0
  60. 0 1 1 1 0
  61. 0 0 0 0 0
  62.  
  63. 2
  64.  
  65.  
  66. 5
  67. 0 1 0 1 1
  68. 1 0 0 1 1
  69. 0 1 0 0 0
  70. 0 0 0 0 0
  71. 1 1 1 1 1
  72.  
  73. 3
  74. */
Advertisement
Add Comment
Please, Sign In to add comment