您的位置:首页 > 其它

POJ-2935 Basic Wall Maze

2014-08-05 10:20 423 查看
[align=center]Basic Wall Maze[/align]

Time Limit: 1000MS Memory Limit: 65536K
Description

In this problem you have to solve a very simple maze consisting of:

a 6 by 6 grid of unit squares
3 walls of length between 1 and 6 which are placed either horizontally or vertically to separate squares

one start and one end marker
A maze may look like this:


You have to find a shortest path between the square with the start marker and the square with the end marker. Only moves between adjacent grid squares are allowed; adjacent means that the grid squares share an edge and are not separated by a wall. It is
not allowed to leave the grid.

Input

The input consists of several test cases. Each test case consists of five lines: The first line contains the column and row number of the square with the start marker, the second line the column and row number of the square with the end marker. The third,
fourth and fifth lines specify the locations of the three walls. The location of a wall is specified by either the position of its left end point followed by the position of its right end point (in case of a horizontal wall) or the position of its upper end
point followed by the position of its lower end point (in case of a vertical wall). The position of a wall end point is given as the distance from the left side of the grid followed by the distance from the upper side of the grid.

You may assume that the three walls don’t intersect with each other, although they may touch at some grid corner, and that the wall endpoints are on the grid. Moreover, there will always be a valid path from the start marker to the end marker. Note that
the sample input specifies the maze from the picture above.

The last test case is followed by a line containing two zeros.

Output

For each test case print a description of a shortest path from the start marker to the end marker. The description should specify the direction of every move (‘N’ for up, ‘E’ for right, ‘S’ for down and ‘W’ for left).

There can be more than one shortest path, in this case you can print any of them.

Sample Input
1 6
2 6
0 0 1 0
1 5 1 6
1 5 3 5
0 0


Sample Output
NEEESWW

————————————————————集训5.1的分割线————————————————————
前言:直到Roll神告诉我采用USACO的The Castle的方法把四堵墙位压成一个int为止,我这题竟然因为不明原因(G++ AC but C++ WA)Debug了一晚上。

思路:四堵墙位压成1 2 4 8,或运算即可满足要求。然后就是一个裸的BFS+输出路径。

输出路径可以另外开几个数组。fa[][]数组记录父结点,last_d[][]数组记录方向,dir[]保存解反向输出。(倒走迷宫)

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
#define LIM (nx > 0 && nx <= 6 && ny > 0 && ny <= 6)
//up = 1 right = 2 down = 4 left = 8
int sx, sy, ex, ey;
const int d[][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
int mat[8][8], q[100], fa[8][8], dir[100], last_d[8][8];
bool vis[8][8];
char name[4] = {'N', 'E', 'S', 'W'};
void PR(int x, int y)
{
int c = 0;
while(fa[x][y] != -1) {
int fx = fa[x][y] / 10, fy = fa[x][y] % 10;
dir[c++] = last_d[x][y];
x = fx;
y = fy;
}
while(c--) {
printf("%c", name[dir[c]]);
}
puts("");
}

void bfs()
{
q[0] = sx * 10 + sy;
vis[sx][sy] = true;
fa[sx][sy] = -1;
int fron = 0, rear = 1;
while(fron < rear) {
int u = q[fron];
int x = u / 10, y = u % 10;
for(int dd = 0; dd < 4; dd++) {
int nx = x + d[dd][0], ny = y + d[dd][1];
if(LIM && !vis[nx][ny] && !(mat[x][y] & (1<<dd))) {
vis[nx][ny] = true;
fa[nx][ny] = u;
last_d[nx][ny] = dd;
if(nx == ex && ny == ey) {
PR(ex, ey);
return ;
}
q[rear++] = nx*10 + ny;
}
}
fron++;
}
}

int main()
{
while(scanf("%d%d", &sx, &sy), sx) {
scanf("%d%d", &ex, &ey);
memset(vis, 0, sizeof(vis));
memset(mat, 0, sizeof(mat));
int a, b, c, d;
for(int w = 0; w < 3; w++) {
scanf("%d%d%d%d", &a, &b, &c, &d);
if(a == c) {
for(int i = b+1; i <= d; i++) {
mat[a][i] |= 2;
mat[a+1][i] |= 8;
}
}
else {
for(int i = a+1; i <= c; i++) {
mat[i][b] |= 4;
mat[i][b+1] |= 1;
}
}
}
bfs();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: