poj 2049 Finding Nemo

原题:

Finding Nemo
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 8974 Accepted: 2114

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

题意:

题目给出一个网格图, 有墙,有门,有空地。告诉你墙和门的数量和位置,然后让你从(0,0)点出发,找到nemo的位置。
要求是要穿过尽可能少的门。

思路:

一看便知这是一道在求最短路径的题目。没边信息的地图求最短路径比较适合用BFS方法解决。

需要注意的一点是,该题目要求不是最短的步数,而是通过的最少的门数。所以我们在建队列时应该使用以通过门的数量排列的优先队列。

具体做法是:用H数组储存平行与X轴的网格,L数组储存平行与Y轴的网格信息。H[x][y]代表的是(x,y)点下方的网格,L[x][y]则代表(x,y)点左边的网格。如果是空地,赋值为0;如果是门,赋值为1;如果是墙,赋值为无穷。

dis数组储存从源点走到该点需要通过多少门。初始化为无穷,即到所有点都是不通的,只有源点本身的dis值为0.然后每走到下一个点时判断上一个点的dis值与走到该点需要通过的网格的值的相加和是否大于这个点的dis值,如果是则更新该点的dis值,并加入优先队列。

代码:

#include <iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;

const int inf = 0x3f3f3f3f;

struct node	//其实结构体的储存内容跟dis数组是一样的,这里结构体只是为了便于创建优先队列。
{
	int x, y, d;
	bool operator<(const node &other)const
	{
		return d > other.d;		//根据穿过门的数量排序
	}
};		
int H[250][250], L[250][250], dis[250][250]; //下网格,左网格,走过的门数
int sx, sy;		//终点坐标

void bfs(int x, int y)
{
	priority_queue<node>q;
	memset(dis,inf,sizeof(dis));	//初始化为无穷大
	dis[x][y] = 0;
	node cur;
	cur.x = 0, cur.y = 0, cur.d = 0;
	q.push(cur);
	while(!q.empty())
	{
		x = q.top().x;
		y = q.top().y;
		q.pop();
		if(x == sx&&y == sy)
			return;
		if(y<200&&dis[x][y+1] > dis[x][y]+H[x][y+1])//向上走
		{
			dis[x][y+1] = dis[x][y]+H[x][y+1];
			cur.x = x, cur.y = y+1, cur.d = dis[x][y+1];
			q.push(cur);
		}
		if(y > 0&&dis[x][y-1] > dis[x][y] + H[x][y])//向下走
		{
			dis[x][y-1] = dis[x][y]+H[x][y];
			cur.x = x, cur.y = y-1, cur.d = dis[x][y-1];
			q.push(cur);
		}
		if(x>0&&dis[x-1][y] > dis[x][y]+L[x][y])//向左走
		{
			dis[x-1][y] = dis[x][y]+L[x][y];
			cur.x = x-1, cur.y = y, cur.d = dis[x-1][y];
			q.push(cur);
		}
		if(x<200&&dis[x+1][y] > dis[x][y]+L[x+1][y])//向右走
		{
			dis[x+1][y] = dis[x][y]+L[x+1][y];
			cur.x = x+1, cur.y = y, cur.d = dis[x+1][y];
			q.push(cur);
		}
	}
	dis[sx][sy] = -1;		//如果没有返回,说明找不到
}

int main()
{
    int M, N;
    while(cin>>M>>N,N!=-1&&M!=-1)
	{
		int x, y, d, t;
		memset(H,0,sizeof(H));
		memset(L,0,sizeof(L));
		while(M--)
		{
			cin>>x>>y>>d>>t;
			if(!d)
				for(int i = 0;i < t;i++)
					H[x+i][y] = inf;
			else
				for(int j = 0;j < t;j++)
					L[x][y+j] = inf;
		}
		while(N--)
		{
			cin>>x>>y>>d;
			if(!d)
				H[x][y] = 1;
			else
				L[x][y] = 1;
		}
		double f1, f2;
		cin>>f1>>f2;
		sx = (int)f1, sy = (int)f2;
		bfs(0,0);
		cout<<dis[sx][sy]<<endl;
	}
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值