关闭

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

标签: poj2935poj 2935bfs
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

如果去掉障碍  那么就是一道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
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

hdu2191 多重背包(2016xynu暑期集训检测 -----B题)

Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是...
  • su20145104009
  • su20145104009
  • 2016-08-31 13:19
  • 1186

hdu1873 看病要排序 (2016xynu暑期集训检测 -----E题)

Description 看病要排队这个是地球人都知道的常识。 不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重...
  • su20145104009
  • su20145104009
  • 2016-08-31 13:40
  • 1262

2015-2017机器人操作系统(ROS)及其应用暑期学校资料汇总 ROS Summer School 持续更新

综合信息:2015     2016 课程资料:2015     2016 全部课程视频链接:智能机器人运动与视觉实验室
  • ZhangRelay
  • ZhangRelay
  • 2016-07-27 09:08
  • 3859

poj2287 Tian Ji -- The Horse Racing(2016xynu暑期集训检测 -----C题)

Description Here is a famous story in Chinese history. That was about 2300 years ago. General T...
  • su20145104009
  • su20145104009
  • 2016-08-31 13:31
  • 1423

POJ-2935-Basic Wall Maze

这个题用BFS搜索,题目大意是给你6*6的网格,其中第一行的2个数据表示起点,第二行的2个数据表示终点,第三行开始有3行,表示一道墙(可以当作一条线),有墙的地方相应不能通过。主要就是对墙需要特殊处理...
  • z309241990
  • z309241990
  • 2013-02-17 02:07
  • 675

POJ 2935 Basic Wall Maze BFS

注意墙的处理,我是这么做的,把每个方块不能行走的方向标记出来,剩他的就是传统BFS了。 #include #include using namespace std; int sx,sy,ex,ey;...
  • waitfor_
  • waitfor_
  • 2012-01-14 13:49
  • 757

POJ2935 Basic Wall Maze(BFS)

Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2963  ...
  • wust_xhj
  • wust_xhj
  • 2015-08-16 17:00
  • 220

POJ 2935 Basic Wall Maze

POJ 2935 Basic Wall Maze 1 算法 BFS 2 代码 #Source Code 1 #Problem: 2935 User: wuyubin #Memory: N/A ...
  • electric_engine
  • electric_engine
  • 2015-03-09 01:31
  • 248

POJ-2935 Basic Wall Maze

Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K     Special Judge ...
  • u012325552
  • u012325552
  • 2014-08-05 10:20
  • 533

poj 2935 Basic Wall Maze

#include #include #include #include using namespace std; struct node { int x, y; strin...
  • qqchenjunwei
  • qqchenjunwei
  • 2014-04-15 21:13
  • 553
    个人资料
    • 访问:1398214次
    • 积分:20787
    • 等级:
    • 排名:第446名
    • 原创:601篇
    • 转载:18篇
    • 译文:0篇
    • 评论:376条
    你只管努力 其它交给时光

    点图片联系我


    唐唐女神

    点图片联系我

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