Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int cnt = 0;
- char area[22][22];
- int visited[22][22];
- int columns, rows;
- void dfs(int r, int c)
- {
- if (visited[r][c] == 1 || r <= 0 || c <= 0 || r > rows || c > columns)
- {
- return;
- }
- if (area[r][c] == '#')
- {
- return;
- }
- if (area[r][c] == '.' || area[r][c] == '@')
- {
- cnt++;
- }
- visited[r][c] = 1;
- dfs(r + 1, c);
- dfs(r - 1, c);
- dfs(r, c + 1);
- dfs(r, c - 1);
- }
- int main()
- {
- int t, r, c;
- cin >> t;
- for (int q = 1; q <= t; q++)
- {
- cin >> columns >> rows;
- for (int i = 1; i <= rows; i++)
- {
- for (int j = 1; j <= columns; j++)
- {
- cin >> area[i][j];
- if (area[i][j] == '@')
- {
- r = i;
- c = j;
- }
- }
- }
- dfs(r, c);
- cout << "Case " << q << ": " << cnt << endl;
- cnt = 0;
- memset(visited, 0, sizeof(visited));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement