Advertisement
El_GEMMY

flood fill #2

Sep 11th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define ull unsigned long long
  4. #define s second
  5. #define f first
  6. #include<vector>
  7. #include<set>
  8. #include<map>
  9. #include<stack>
  10. const ll Mod = 1000000007;
  11. using namespace std;
  12. void salma(){
  13.     ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  14.     cout << fixed << setprecision(0);
  15. }
  16. int n , m , cnt = 0;
  17.  
  18. vector<vector<char>> grid;
  19. vector<vector<bool>> vis;
  20.  
  21.  
  22. bool valid(int r , int c){
  23.     return r >= 0 && r < n && c >= 0 && c < m;
  24. }
  25.  
  26. int x , y;
  27. void reachCells(int r , int c){
  28. //    if(valid(r , c) && grid[r][c] == '*' && r == x && c == y)  grid[r][c] = '.' , cnt--;
  29.  
  30.     if(!valid(r , c) || grid[r][c] == '*' || vis[r][c] == 1) return;
  31.     vis[r][c] = true;
  32.     cnt++;
  33.  
  34.     reachCells(r , c - 1);
  35.     reachCells(r , c + 1);
  36.     reachCells(r - 1 , c);
  37.     reachCells(r + 1 , c);
  38.  
  39. }
  40.  
  41. int main(){
  42. //    freopen("input.txt", "r", stdin);
  43. //    freopen("output.txt", "w", stdout);
  44.     salma();
  45.     cin >> n >> m;
  46.     grid.assign(n , vector<char> (m));
  47.     vis.resize(n , vector<bool> (m));
  48.     for(int i = 0 ; i < n ; i++){
  49.         for(int j = 0 ; j < m; j++){
  50.             cin >> grid[i][j];
  51.         }
  52.     }cin >> x >> y;
  53.  
  54.     reachCells(x - 1, y - 1);
  55.     cout << cnt;
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement