USACO Overfencing

Code

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
/*
ID: wcr19961
PROG: maze1
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long LL;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const int maxh = 210;
const int maxw = 110;
char g[maxh][maxw];
int len[maxh][maxw];
struct node {
int x, y;
node(int x = 0, int y = 0) : x(x), y(y) {}
};
int main() {
freopen("maze1.in", "r", stdin);
freopen("maze1.out", "w", stdout);
int w, h;
scanf("%d%d%*c", &w, &h);
for (int i = 0; i <= (h << 1); ++i) {
fgets(g[i], maxw, stdin);
}
queue<node> q;
for (int i = (w << 1) - 1; i > 0; i -= 2) {
if (g[0][i] == ' ') {
q.push(node(1, i));
len[1][i] = 1;
}
if (g[h << 1][i] == ' ') {
q.push(node((h << 1) - 1, i));
len[(h << 1) - 1][i] = 1;
}
}
for (int i = (h << 1) - 1; i > 0; i -= 2) {
if (g[i][0] == ' ') {
q.push(node(i, 1));
len[i][1] = 1;
}
if (g[i][w << 1] == ' ') {
q.push(node(i, (w << 1) - 1));
len[i][(w << 1) - 1] = 1;
}
}
int ans = 1;
while (!q.empty()) {
node k = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {
int x = k.x + dx[i], y = k.y + dy[i];
if (g[x][y] == ' ') {
x += dx[i], y += dy[i];
if (x > 0 && x < (h << 1) && y > 0 && y < (w << 1) && len[x][y] == 0) {
len[x][y] = len[k.x][k.y] + 1;
ans = max(ans, len[x][y]);
q.push(node(x, y));
}
}
}
}
printf("%d\n", ans);
return 0;
}

支付宝扫码领红包