关闭

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

标签: poj2935poj 2935bfs
1143人阅读 评论(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

如果去掉障碍  那么就是一道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;
}

  

1
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1208421次
    • 积分:18918
    • 等级:
    • 排名:第463名
    • 原创:594篇
    • 转载:17篇
    • 译文:0篇
    • 评论:334条
    你只管努力 其它交给时光

    点图片联系我


    唐唐女神

    点图片联系我

    访问统计
    Flag Counter
    博客专栏
    最新评论