###### poj2935 Basic Wall Maze （2016xynu暑期集训检测 -----D题）

Description

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

1. a 6 by 6 grid of unit squares
2. 3 walls of length between 1 and 6 which are placed either horizontally or vertically to separate squares
3. 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

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int st_x,st_y;
int ed_x,ed_y;
//wall[i][j][k][l]  表示(i,j)与(k,l)之间有障碍
bool wall[10][10][10][10];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
char Dir[5]={"EWSN"};
bool vis[10][10];
bool judge(int x,int y)
{
if(x<=0||y<=0||x>6||y>6||vis[x][y])
return false;
return true;
}
struct node
{
int step;
int index;
int x,y;
char path[100];
friend bool operator<(node x1,node y1)
{
return x1.step>y1.step;
}
};
void bfs()
{
memset(vis,false,sizeof(vis));
priority_queue<node>s;
node temp1,temp2;
temp1.x=st_x;temp1.y=st_y;temp1.step=0;temp1.index=0;
s.push(temp1);
while(!s.empty())
{
temp1=temp2=s.top();s.pop();
if(temp1.x==ed_x&&temp1.y==ed_y)
break;
if(vis[temp1.x][temp1.y])
continue;
vis[temp1.x][temp1.y]=true;
for(int i=0;i<4;i++)
{
int xx=temp1.x+dir[i][0];
int yy=temp1.y+dir[i][1];
if(judge(xx,yy)&&!wall[temp1.x][temp1.y][xx][yy])
{
temp1.x=xx;
temp1.y=yy;
temp1.path[temp1.index++]=Dir[i];
temp1.path[temp1.index]='\0';
temp1.step++;
s.push(temp1);
}
temp1=temp2;
}
}
while(!s.empty())
s.pop();
printf("%s\n",temp1.path);
}
int main()
{
while(~scanf("%d %d",&st_x,&st_y))
{
if(!st_x&&!st_y)
break;
scanf("%d %d",&ed_x,&ed_y);
memset(wall,false,sizeof(wall));
for(int i=0;i<3;i++)
{
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
//处理障碍
if(a==c)
{
int l=min(b,d);
int r=max(b,d);
for(int j=l+1;j<=r;j++)
{
wall[a][j][a+1][j]=true;
wall[a+1][j][a][j]=true;
}
}
//处理障碍
if(b==d)
{
int l=min(a,c);
int r=max(a,c);
for(int j=l;j<r;j++)
{
wall[j+1][b][j+1][b+1]=true;
wall[j+1][b+1][j+1][b]=true;
}
}
}
bfs();
}
return 0;
}

#### poj2935 Basic Wall Maze

2014-01-17 21:11:22

#### Basic Wall Maze

2015-04-21 16:07:56

#### POJ2935 Basic Wall Maze(BFS)

2015-08-16 17:00:31

#### HDU 1484 Basic wall maze (dfs + 记忆化)

2014-05-08 18:07:52

#### POJ2935 Basic Wall Maze bfs记录路径

2015-01-20 15:09:46

#### 22 poj2935 Basic Wall Maze --- bfs

2015-05-18 22:09:04

#### POJ2935 Basic Wall Maze bfs+模拟

2011-09-01 22:28:57

#### 2017ACM暑假集训总结

2017-09-04 20:44:47

#### 2017ACM暑假训练总结

2017-08-26 22:26:25

#### 南开大学ACM暑期集训之 组合数学

2010年05月24日 636KB 下载

## 不良信息举报

poj2935 Basic Wall Maze （2016xynu暑期集训检测 -----D题）