-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lakes uva 722
53 lines (43 loc) · 1.14 KB
/
Lakes uva 722
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
char grid[105][105];
bool visited[105][105];
int r, c, p;
int water_count;
void dfs(int x, int y) {
if (x < 0 || x >= r || y < 0 || y >= c || grid[x][y] == '1' || visited[x][y]) {
return;
}
visited[x][y] = true;
water_count++;
// Move in 4 possible directions: up, right, down, left
dfs(x - 1, y);
dfs(x, y + 1);
dfs(x + 1, y);
dfs(x, y - 1);
}
int main() {
int t;
cin >> t;
while (t--) {
cin >> r >> c >> p;
memset(grid, 0, sizeof(grid));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> grid[i][j];
}
}
for (int k = 0; k < p; k++) {
int startX, startY;
cin >> startX >> startY;
memset(visited, 0, sizeof(visited)); // Reset visited for each new position
water_count = 0;
dfs(startX-1, startY-1);
cout << water_count << endl;
}
cout << endl; // Print a blank line between datasets
}
return 0;
}