苏苏爱自由

生如夏花之绚烂,死如秋叶之静美。

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

如果去掉障碍  那么就是一道dijkstra的最短路径  题

既然有了障碍  那么就处理障碍 

把障碍相隔的两个点  设置为不能通过


剩下就是bfs了

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

  
阅读更多
版权声明:本文为博主原创文章 http://blog.csdn.net/su20145104009 https://blog.csdn.net/su20145104009/article/details/52384073
文章标签: poj2935 poj 2935 bfs
个人分类: 【搜索】
所属专栏: Acm竞赛
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

poj2935 Basic Wall Maze (2016xynu暑期集训检测 -----D题)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭