-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgold.cc
More file actions
88 lines (69 loc) · 1.54 KB
/
gold.cc
File metadata and controls
88 lines (69 loc) · 1.54 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// William Sjöblom
#include <cstdio>
#include <utility>
#include <set>
int start_x, start_y;
int w, h;
char map[50][50];
bool visited[50][50];
/**
* Read map from stdin.
*/
void scan_map() {
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
scanf("%c", &map[x][y]);
if (map[x][y] == 'P') {
start_x = x;
start_y = y;
}
}
scanf("\n");
}
}
/**
* Write map to stdout.
*/
void print_map() {
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
printf("%c", map[x][y]);
}
printf("\n");
}
}
/**
* Returns 'true' if the player feels a draft at the
* current position.
*/
inline bool senses_draft(int x, int y) {
return (map[x + 1][y] == 'T' ||
map[x - 1][y] == 'T' ||
map[x][y + 1] == 'T' ||
map[x][y - 1] == 'T');
}
/**
* Solve.
*/
int solve(int x, int y) {
if (x < 0 || y < 0 || x >= w || y >= h) return 0;
if (visited[x][y]) return 0;
visited[x][y] = true;
if (map[x][y] == '#' ||
map[x][y] == 'T') return 0;
int result = 0;
if (!senses_draft(x, y)) {
result += solve(x + 1, y);
result += solve(x - 1, y);
result += solve(x, y + 1);
result += solve(x, y - 1);
}
if (map[x][y] == 'G') return result + 1;
return result;
}
int main() {
scanf("%d %d\n", &w, &h);
scan_map();
int result = solve(start_x, start_y);
printf("%d\n", result);
}