Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //problem link: https://lightoj.com/problem/guilty-prince
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- const int mx = 30;
- char grid[mx][mx];
- bool vis[mx][mx];
- int dx[] = {0, 0, 1, -1};
- int dy[] = {1, -1, 0, 0};
- int res = 0;
- int m, n;
- void dfs(int x, int y) {
- vis[x][y] = true;
- res++;
- for (int i = 0; i < 4; i++) {
- int x1 = x + dx[i];
- int y1 = y + dy[i];
- if (x1 >= 1 && x1 <= n && y1 >= 1 && y1 <= m && !vis[x1][y1] &&
- grid[x1][y1] != '#') {
- dfs(x1, y1);
- }
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int tt, k = 1;
- cin >> tt;
- while (tt--) {
- memset(vis, 0, sizeof(vis));
- res = 0;
- cin >> m >> n;
- int x = 0, y = 0;
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= m; j++) {
- cin >> grid[i][j];
- if (grid[i][j] == '@') {
- x = i, y = j;
- }
- }
- }
- dfs(x, y);
- cout << "Case " << k++ << ": " << res << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement