POJ 2049 Finding Nemo (bfs+优先队列)

Finding Nemo
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 6869 Accepted: 1571

Description

Nemo is a naughty boy. One day he went into the deep sea all by himself. Unfortunately, he became lost and couldn't find his way home. Therefore, he sent a signal to his father, Marlin, to ask for help.
After checking the map, Marlin found that the sea is like a labyrinth with walls and doors. All the walls are parallel to the X-axis or to the Y-axis. The thickness of the walls are assumed to be zero.
All the doors are opened on the walls and have a length of 1. Marlin cannot go through a wall unless there is a door on the wall. Because going through a door is dangerous (there may be some virulent medusas near the doors), Marlin wants to go through as few doors as he could to find Nemo.
Figure-1 shows an example of the labyrinth and the path Marlin went through to find Nemo.

We assume Marlin's initial position is at (0, 0). Given the position of Nemo and the configuration of walls and doors, please write a program to calculate the minimum number of doors Marlin has to go through in order to reach Nemo.

Input

The input consists of several test cases. Each test case is started by two non-negative integers M and N. M represents the number of walls in the labyrinth and N represents the number of doors.
Then follow M lines, each containing four integers that describe a wall in the following format:
x y d t
(x, y) indicates the lower-left point of the wall, d is the direction of the wall -- 0 means it's parallel to the X-axis and 1 means that it's parallel to the Y-axis, and t gives the length of the wall.
The coordinates of two ends of any wall will be in the range of [1,199].
Then there are N lines that give the description of the doors:
x y d
x, y, d have the same meaning as the walls. As the doors have fixed length of 1, t is omitted.
The last line of each case contains two positive float numbers:
f1 f2
(f1, f2) gives the position of Nemo. And it will not lie within any wall or door.
A test case of M = -1 and N = -1 indicates the end of input, and should not be processed.

Output

For each test case, in a separate line, please output the minimum number of doors Marlin has to go through in order to rescue his son. If he can't reach Nemo, output -1.

Sample Input

8 9
1 1 1 3
2 1 1 3
3 1 1 3
4 1 1 3
1 1 0 3
1 2 0 3
1 3 0 3
1 4 0 3
2 1 1
2 2 1
2 3 1
3 1 1
3 2 1
3 3 1
1 2 0
3 3 0
4 3 1
1.5 1.5
4 0
1 1 0 1
1 1 1 1
2 1 1 1
1 2 0 1
1.5 1.7
-1 -1

Sample Output

5
-1

Source

 
 
菜鸟一枚。。。。nemo都要被我饿死了。。。。
各种小错误。。。
 
注意:
1、各种情况要考虑的全面一点(居然第一次写的时候忘了是空气也要入队),组织分支语句的时候也要注意if(),else if(),else()之类。。。有助于自己理清逻辑。。。
2、最好不写if(){}+else if(){}+什么都没有。。。。即使没有了,也最好写个else{}(有助于我这种有时候逻辑有点小混乱的人哈。。。)
3、变量什么的不要搞混了。。。
4、一定要考虑墙没有和nemo在整个board外的情况
 
感想:
这种以“格子”作为搜索单元的题好像很少,注意!(以前见过的都是以"点"作为搜索单元)
 
代码:
//将一个格子看做一个搜索的单元
//每个格子看成一个房间,然后房间有四面(可以是墙、门或者空气)
//map[][][]前两维存坐标,后一维存每个格子的四面(即最后一维表示方向)
//map[][][]可以是-1\0\1   (0=墙,1=门,-1代表什么都没有,空地或者空气什么的)
//vis[][][]也开的三维,意义和maps[][][]的一样,只是用来标记用的(走过为1,没走过为0)
#include<cstdio>
#include<cstring>
#include<queue>
#define WALL 0
#define door 1
#define air -1
#define maxx(a,b) (a)>(b)?(a):(b)
#define minx(a,b) (a)<(b)?(a):(b)

using namespace std;

struct node
{
    int x;
    int y;
    int step;
	bool operator <(const node &a)const    //定义什么叫"小"
    {
        return step>a.step;               //步数大的优先级小
    }
};
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int vis[205][205][4];
int maps[205][205][4];     
int ans;                    //找到nemo穿过的最少门数
int n,m;
int sx,sy;

//判断是否越出范围,这题有点特殊,上界要在输入数据时不断更新
bool isok(node &a)    
{
    if(a.x<=m+1&&a.x>0&&a.y<=n+1&&a.y>0)
        return true;
    return false;
}

void debug(node &a)     //调试函数,囧
{
	printf("x=%d y=%d step=%d\n",a.x,a.y,a.step);
}

void bfs()
{
	int i,dd,step;
    priority_queue<node> q;   
    while(!q.empty())
        q.pop();
    node tmp,cur;
    cur.x=1;        //马林初始在(0,0),也就是(1,1)这个格子
    cur.y=1; 
    cur.step=0;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.top();
		q.pop();
        for(i=0;i<4;i++)    
        {
			tmp.x=cur.x+dx[i];
			tmp.y=cur.y+dy[i];
			if(!vis[cur.x][cur.y][i]&&isok(tmp))
			{
				if(maps[cur.x][cur.y][i]==WALL)
					continue;
				else
				{
					if(maps[cur.x][cur.y][i]==door)
						step=1;            //是门step+1再入队
					else  step=0;          //是空气step不变直接入队
					if(i==0||i==1)        //由于格子相邻,所以其边是相邻格子的对边
						dd=1-i;
					else
						dd=5-i;           //算dd是为了右面标记vis[][][]用的
					vis[tmp.x][tmp.y][dd]=1;
					vis[cur.x][cur.y][i]=1;
					tmp.step=cur.step+step;
					q.push(tmp);
					//debug(tmp);
					if(tmp.x==sx+1&&tmp.y==sy+1)
						ans=minx(tmp.step,ans);
				}
			}
		}
	}
	if(ans<0x3f3f3f3f)
		printf("%d\n",ans);
	else
		printf("-1\n");
}

int main()
{
    double aa,bb;
    int t1,t2;
    int i,j,k;
    int x,y,d,t;
    int x1,y1,d1;
    while(scanf("%d%d",&t1,&t2)!=EOF)
    {
		if(t1+t2==-2)
            break;
        memset(maps,-1,sizeof(maps));
        memset(vis,0,sizeof(vis));
        ans=0x3f3f3f3f;
        for(i=0;i<t1;i++)
        {
            scanf("%d%d%d%d",&x,&y,&d,&t);
			getchar();
            if(d==0)
            {
                for(k=1;k<=t;k++)
                {
                    maps[x+k][y+1][1]=WALL;     //格子的边相邻,所以左格子的右边即是右格子的左边
                    maps[x+k][y][0]=WALL;       //所以maps[][][]上都要标记一下
                }
                m=maxx(m,x+t);
				n=maxx(n,y);
            }
            else
            {
                for(k=1;k<=t;k++)
                {
                    maps[x+1][y+k][2]=WALL;
                    maps[x][y+k][3]=WALL;
                }
				m=maxx(m,x);
                n=maxx(n,y+t);
            }
        }
		t1=1;
        for(j=0;j<t2;j++)
        {
            scanf("%d%d%d",&x1,&y1,&d1);
			getchar();
            if(d1==0)
            {
				for(k=1;k<=t1;k++)
				{
					maps[x1+k][y1+1][1]=door;
					maps[x1+k][y1][0]=door;
				}
            }
            else
            {
				for(k=1;k<=t1;k++)
				{
					maps[x1+1][y1+k][2]=door;
					maps[x1][y1+k][3]=door;
				}
            }
        }
        scanf("%lf%lf",&aa,&bb);	
		if(m==0&&n==0)                         //没有墙也没有门
			printf("0\n");
		else if(aa<1||aa>199||bb<1||bb>199)    //nemo在整个墙外
			printf("0\n");                     //题目说墙的两端介于[1,199]
		else
		{
			sx=int(aa);
			sy=int(bb);
			if(aa<=0||aa>m||bb<=0||bb>n)
				printf("0\n");
			bfs();
		}
    }
    return 0;
}



/*
4 1
3 2 1 1
1 3 0 2
1 1 0 2
1 1 1 2
2 1 1
1.5 1.5

output=0
*/

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值