关闭

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

121人阅读 评论(0) 收藏 举报
分类:
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;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:94179次
    • 积分:3970
    • 等级:
    • 排名:第8292名
    • 原创:317篇
    • 转载:2篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论