poj--2935(bfs+记录路径)

原创 2015年11月19日 21:32:54
Basic Wall Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3012   Accepted: 1355   Special Judge

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



解体思路:这是我第一道关于记录路径的题目,感觉有点难.花了4个小时终于搞懂了.会了这道题目就可以写hdu--1226了.我认为理解的关键,主要在于理解bfs时,标记过的点不可以再走,所以每一个结点(情况)回溯时只有一个双亲结点.那么我们就可以记录每一个结点的前驱,存储在pre中,最后找到最短路径后,向前回溯就行了.


代码如下:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct stu
{
	int x,y,pre,way;
};
stu s,e;
stu Que[1100];
int mark[110][110];
char step[1100];
int vist[15][15];
int dis[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右 
int  check(stu temp,int i)
{
	int r,l;
	if(temp.x<1||temp.y<1||temp.x>6||temp.y>6||vist[temp.x][temp.y])
	return 0;
	if(i==0)
	{
		r=temp.x*10+temp.y-1;
		if(mark[r][r+1])
		return 0;
	}
	else if(i==1)
	{
		r=(temp.x-1)*10+temp.y-1;
		l=(temp.x-1)*10+temp.y;
		if(mark[r][l])
		return 0;
	}
	else if(i==2)
	{
		r=(temp.x-1)*10+temp.y;
		l=temp.x*10+temp.y;
		if(mark[r][l])
		return 0;
	}
	else if(i==3)
	{
	   r=(temp.x-1)*10+temp.y-1;
	    l=temp.x*10+temp.y-1;
	    if(mark[r][l])
	    return 0;
	}
	return 1;
}
void echo(int i)
{
	if(Que[i].pre==-1)return;
	else echo(Que[i].pre);
	if(Que[i].way==0)
	printf("N");
	else if(Que[i].way==1)
	printf("S");
	else if(Que[i].way==2)
	printf("W");
	else if(Que[i].way==3)
	printf("E");
}
void bfs()
{
	int front,rear;
	stu curr,temp;
	front=0;
	rear=1;
	Que[front]=s;
	while(front!=rear)
	{
		curr=Que[front];
		for(int i=0;i<4;i++)
		{
			temp.x=curr.x+dis[i][0];
			temp.y=curr.y+dis[i][1];
			if(check(temp,i))
			{
				Que[rear].x=temp.x;
				Que[rear].y=temp.y;
				Que[rear].pre=front;
				Que[rear].way=i;
			    if(temp.x==e.x&&temp.y==e.y)
				{
				  echo(rear);
				  puts("");
				  return;
				}
				else 
				{
					vist[temp.x][temp.y]=1;
					rear++;
				}
				
			}
		}
		front++;	
	}
}
int main()
{
	int n,m,n1,n2,m1,m2,t;
	while(scanf("%d%d",&m,&n),m|n)
	{
		memset(mark,0,sizeof(mark));
		s.x=n;s.y=m;//行列 
		s.pre=-1;
		s.way=-1;
	    scanf("%d%d",&m,&n);
	    e.x=n;e.y=m;
	    for(int i=1;i<=3;i++)
	    {
	    	scanf("%d%d",&m1,&n1);//m为烈,n为行 
	    	scanf("%d%d",&m2,&n2);
	        if(m1>m2)//同一行 
	        {
	        	t=m1;
	        	m1=m2;
	        	m2=t;	
	        }
	        else if(n1>n2)//同一列 
	        {
	        	t=n1;
	        	n1=n2;
	        	n2=t;
	        }
	        int k,k1;
	        if(n1==n2)//同一行 
	        {
	        	k=n1*10+m1;
	        	for(int i=m1+1;i<=m2;i++)
	        	{
	        		k1=0;
	        		k1=n2*10+i;
	        		mark[k][k1]=1;
	        		k=k1;
	        	}
	        }
	        else if(m1==m2)
	        {
	        	k=n1*10+m1;
	        	for(int i=n1+1;i<=n2;i++)
	        	{
	        		k1=0;
	        		k1=i*10+m2;
	        		mark[k][k1]=1;
	        		k=k1;
	        	}
	        }
		}
		memset(vist,0,sizeof(vist));
		vist[s.x][s.y]=1;
		bfs();
	}
	return 0;
}


POJ2935 Basic Wall Maze bfs记录路径

链接:   POJ2935 题意: 6 X 6的地图   格子和格子可能有墙      整个地图中有三道墙      求起点起点到终点的路径 本题中的墙可以理解为...
  • AXuan_K
  • AXuan_K
  • 2015年01月20日 15:09
  • 781

POJ 2935 Basic Wall Maze BFS

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

POJ2935 Basic Wall Maze(BFS)

Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2963  ...

poj Pots(BFS)(路径记录)(模拟)

Pots Description You are given two pots, having the volume of A and B liters respectively. The fo...

POJ3414BFS+路径记录

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17841   Accepted...

poj 3984 迷宫问题(BFS,记录路径)

题目链接http://poj.org/problem?id=3984 Bfs + vis记录路径 #include #include #include #include #include #...
  • mr_zj_
  • mr_zj_
  • 2016年08月05日 12:06
  • 159

poj 3414 Pots (BFS + 记录路径)

题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Tot...

POJ 3414 Pots (BFS记录最短路径)

题目链接POJ3414题目大意给出了两个瓶子1号和2号的容量分别为A和B, 以及一个目标水量C,对两个瓶子有如下操作: FILL(i):把i号瓶子装满 DROP(i) :把i号瓶子倒光 POUR...

poj3984~用bfs求最短路~同时记录路径

今晚因为一个小小而隐蔽的

poj3414有向图的搜索BFS+路径记录

代码偏长只是大体思路 //这道题不能深度回溯的方法,因为倒水的状态是有向图 //那么只能用宽度搜索的方法 //同时我们应该记录路径当找到最优解就退出 //388K 0MS #include ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj--2935(bfs+记录路径)
举报原因:
原因补充:

(最多只允许输入30个字)