一 原题
Kolstad and Schrijvers
Farmer John went crazy and created a huge maze of fences out in a field. Happily, he left out two fence segments on the edges, and thus created two "exits" for the maze. Even more happily, the maze he created by this overfencing experience is a `perfect' maze: you can find a way out of the maze from any point inside it.
Given W (1 <= W <= 38), the width of the maze; H (1 <= H <= 100), the height of the maze; 2*H+1 lines with width 2*W+1 characters that represent the maze in a format like that shown later - then calculate the number of steps required to exit the maze from the `worst' point in the maze (the point that is `farther' from either exit even when walking optimally to the closest exit). Of course, cows walk only parallel or perpendicular to the x-y axes; they do not walk on a diagonal. Each move to a new square counts as a single unit of distance (including the move "out" of the maze.
Here's what one particular W=5, H=3 maze looks like:
+-+-+-+-+-+ | | +-+ +-+ + + | | | | + +-+-+ + + | | | +-+ +-+-+-+
Fenceposts appear only in odd numbered rows and and odd numbered columns (as in the example). The format should be obvious and self explanatory. Each maze has exactly two blank walls on the outside for exiting.
PROGRAM NAME: maze1
INPUT FORMAT
| Line 1: | W and H, space separated |
| Lines 2 through 2*H+2: | 2*W+1 characters that represent the maze |
SAMPLE INPUT (file maze1.in)
5 3 +-+-+-+-+-+ | | +-+ +-+ + + | | | | + +-+-+ + + | | | +-+ +-+-+-+
OUTPUT FORMAT
A single integer on a single output line. The integer specifies the minimal number of steps that guarantee a cow can exit the maze from any possible point inside the maze.SAMPLE OUTPUT (file maze1.out)
9The lower left-hand corner is *nine* steps from the closest exit.
二 分析
三 代码
/*
ID:maxkibb3
LANG:C++
PROG:maze1
*/
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX_H = 100;
const int MAX_W = 38;
struct Point {
int x, y;
};
Point Pt(int _x, int _y) {
Point ret;
ret.x = _x, ret.y = _y;
return ret;
}
int w, h, ans;
char map[2 * MAX_H + 1][2 * MAX_W + 1];
vector<Point> exitp;
int v[2 * MAX_H + 1][2 * MAX_W + 1];
int v_copy[2 * MAX_H + 1][2 * MAX_W + 1];
int dx[4][2] = {{-2, 0}, {2, 0}, {0, -2}, {0, 2}};
void check_exit(Point p) {
if(map[p.x][p.y] == ' ') {
if(p.x == 0) p.x = -1;
else if(p.x == h - 1) p.x = h;
if(p.y == 0) p.y = -1;
else if(p.y == w - 1) p.y = w;
exitp.push_back(p);
}
}
void init() {
scanf("%d%d", &w, &h);
w = 2 * w + 1;
h = 2 * h + 1;
for(int i = 0; i < h; i++) {
getchar();
for(int j = 0; j < w; j++)
scanf("%c", &map[i][j]);
}
}
bool movable(Point p, int i) {
if(p.x + dx[i][0] < 1 || p.x + dx[i][0] > h - 2
|| p.y + dx[i][1] < 1 || p.y + dx[i][1] > w - 2) {
return false;
}
if(map[p.x + dx[i][0] / 2][p.y + dx[i][1] / 2] != ' ') {
return false;
}
else return true;
}
void bfs(Point s) {
memset(v, 0, sizeof(v));
queue<Point> q;
q.push(s);
v[s.x][s.y] = 0;
while(!q.empty()) {
Point p = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
if(!movable(p, i)) continue;
int nx = p.x + dx[i][0], ny = p.y + dx[i][1];
if(v[nx][ny] != 0) continue;
q.push(Pt(nx, ny));
v[nx][ny] = v[p.x][p.y] + 1;
}
}
}
int main() {
freopen("maze1.in", "r", stdin);
freopen("maze1.out", "w", stdout);
init();
for(int i = 1; i < w; i += 2) {
check_exit(Pt(0, i));
check_exit(Pt(h - 1, i));
}
for(int i = 1; i < h; i += 2) {
check_exit(Pt(i, 0));
check_exit(Pt(i, w - 1));
}
bfs(exitp[0]);
memcpy(v_copy, v, sizeof(v));
bfs(exitp[1]);
for(int i = 1; i < h; i += 2) {
for(int j = 1; j < w; j += 2) {
int val = min(v[i][j], v_copy[i][j]);
ans = max(ans, val);
}
}
printf("%d\n", ans);
return 0;
}

本文介绍了一种通过迷宫寻找最远点到出口的最短路径的方法。利用广度优先搜索(BFS)从两个出口分别进行遍历,计算迷宫中任意一点到达最近出口所需的最小步数。
890

被折叠的 条评论
为什么被折叠?



