-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path11906.cpp
72 lines (62 loc) · 1.66 KB
/
11906.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
graphs > traversal
difficulty: medium
date: 28/Feb/2020
hint: consider grid as an implicit graph and walk through it avoiding redundant positions (nr, nc)
by: @brpapa
*/
#include <iostream>
#include <cstring>
#include <set>
using namespace std;
#define mp make_pair
#define UNVISITED 0
#define VISITING 1
#define WATER 2
#define EVEN 3
#define ODD 4
int R, C, M, N;
int grid[110][110];
set<pair<int, int>> d; // dr, dc
void walk(int r, int c) {
if (grid[r][c] != UNVISITED) return;
grid[r][c] = VISITING;
int countAdjReachable = 0;
// para cada distinto adjacente de grid[r][c]
for (pair<int, int> n : d) {
int nr = r+n.first, nc = c+n.second;
if (nr >= 0 && nr < R && nc >= 0 && nc < C && grid[nr][nc] != WATER) {
countAdjReachable++;
walk(nr, nc);
}
}
if (countAdjReachable%2) grid[r][c] = ODD;
else grid[r][c] = EVEN;
}
int main() {
int T; cin >> T;
for (int t = 1; t <= T; t++) {
memset(grid, UNVISITED, sizeof grid);
cin >> R >> C >> M >> N;
int W; cin >> W;
while (W--) {
int r, c; cin >> r >> c;
grid[r][c] = WATER;
}
d.clear();
// descarta possíveis redundâncias
d.insert({
mp(M, N), mp(-M, N), mp(M, -N), mp(-M, -N),
mp(N, M), mp(N, -M), mp(-N, M), mp(-N, -M)
});
walk(0, 0);
int countEven = 0, countOdd = 0;
for (int r = 0; r < R; r++)
for (int c = 0; c < C; c++) {
if (grid[r][c] == EVEN) countEven++;
if (grid[r][c] == ODD) countOdd++;
}
cout << "Case " << t << ": " << countEven << " " << countOdd << endl;
}
return 0;
}