Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- // count reachable
- const int N = 100;
- char a[N][N];
- int n, m;
- bool vis[N][N];
- bool valid (int row, int col) {
- return (row >= 0 && col >= 0 && row < n && col < m);
- }
- int cnt;
- void count (int row, int col) {
- // base case
- if (!valid(row, col) || a[row][col] == 'X' || vis[row][col])
- return;
- // recursive case
- vis[row][col] = true;
- cnt++;
- count(row + 1, col);
- count(row - 1, col);
- count(row, col + 1);
- count(row, col - 1);
- }
- int main () {
- // freopen("/home/hamoudi/clion/hello.in", "rt", stdin);
- cnt = 0;
- cin >> n >> m;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cin >> a[i][j];
- }
- }
- count(0, 0);
- cout << cnt << '\n';
- }
Add Comment
Please, Sign In to add comment