Hamoudi30

count reachable

Jul 29th, 2021 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. // count reachable
  4. const int N = 100;
  5. char a[N][N];
  6. int n, m;
  7. bool vis[N][N];
  8. bool valid (int row, int col) {
  9.     return (row >= 0 && col >= 0 && row < n && col < m);
  10. }
  11. int cnt;
  12. void count (int row, int col) {
  13.     // base case
  14.     if (!valid(row, col) || a[row][col] == 'X' || vis[row][col])
  15.         return;
  16.     // recursive case
  17.     vis[row][col] = true;
  18.     cnt++;
  19.     count(row + 1, col);
  20.     count(row - 1, col);
  21.     count(row, col + 1);
  22.     count(row, col - 1);
  23. }
  24. int main () {
  25. //    freopen("/home/hamoudi/clion/hello.in", "rt", stdin);
  26.     cnt = 0;
  27.     cin >> n >> m;
  28.     for (int i = 0; i < n; ++i) {
  29.         for (int j = 0; j < m; ++j) {
  30.             cin >> a[i][j];
  31.         }
  32.     }
  33.     count(0, 0);
  34.     cout << cnt << '\n';
  35. }
Add Comment
Please, Sign In to add comment