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

1281人阅读 评论(0)

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;
}

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：1398214次
• 积分：20787
• 等级：
• 排名：第446名
• 原创：601篇
• 转载：18篇
• 译文：0篇
• 评论：376条
你只管努力 其它交给时光

点图片联系我

点图片联系我

访问统计
博客专栏
 菜鸟android学习之路 文章：49篇 阅读：144243
 Acm竞赛 文章：234篇 阅读：563785
 java 文章：133篇 阅读：350768
 Spring框架 文章：16篇 阅读：24339
最新评论