POJ2049Finding Nemo(bfs + 构图)
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 8456 | Accepted: 1975 |
Description
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
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
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 题意:先给出n个墙,以x,y为左下角的点d == 1,与
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std; const int MAX = ;
const int INF = 0x3f3f3f3f;
int maxx,maxy,sx,sy;
int h[MAX][MAX],l[MAX][MAX],dis[MAX][MAX];
double f1,f2;
struct node
{
int x,y;
int door;
bool operator<(const node &a) const
{
return a.door < door;
}
}; void bfs()
{
priority_queue<node> q;
while(q.empty() == )
q.pop();
node cur;
cur.x = ;
cur.y = ;
cur.door = ;
for(int i = ; i <= maxx; i++)
{
for(int j = ; j <= maxy; j++)
{
dis[i][j] = INF;
}
}
dis[][] = ;
q.push(cur);
while(q.empty() == )
{
cur = q.top();
q.pop();
int x = cur.x;
int y = cur.y;
if(x == sx && y == sy)
return;
if(y + <= maxy && dis[x][y + ] > dis[x][y] + h[x][y + ])
{
dis[x][y + ] = dis[x][y] + h[x][y + ];
cur.x = x;
cur.y = y + ;
cur.door = dis[x][y + ];
q.push(cur);
}
if(y - >= && dis[x][y - ] > dis[x][y] + h[x][y])
{
dis[x][y - ] = dis[x][y] + h[x][y];
cur.x = x;
cur.y = y - ;
cur.door = dis[x][y - ];
q.push(cur);
}
if(x + <= maxx && dis[x + ][y] > dis[x][y] + l[x + ][y])
{
dis[x + ][y] = dis[x][y] + l[x + ][y];
cur.x = x + ;
cur.y = y;
cur.door = dis[x + ][y];
q.push(cur);
}
if(x - >= && dis[x - ][y] > dis[x][y] + l[x][y])
{
dis[x - ][y] = dis[x][y] + l[x][y];
cur.x = x - ;
cur.y = y;
cur.door = dis[x - ][y];
q.push(cur);
}
}
dis[sx][sy] = -;
}
int main()
{
int n,m,x,y,d,t;
while(scanf("%d%d",&n,&m) != EOF)
{
if(m == - && n == -)
break;
maxx = maxy = -;
memset(h,,sizeof(h));
memset(l,,sizeof(l));
for(int i = ; i < n; i++)
{
scanf("%d%d%d%d",&x,&y,&d,&t);
if(d == )
{
for(int j = ; j < t; j++)
{
h[x + j][y] = INF;
}
maxx = max(maxx,x + t);
maxy = max(maxy,y);
}
else
{
for(int j = ; j < t; j++)
l[x][y + j] = INF;
maxx = max(maxx,x);
maxy = max(maxy,y + t);
}
}
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &x, &y,&d);
if(d == )
{
h[x][y] = ;
}
else
l[x][y] = ;
}
scanf("%lf%lf",&f1,&f2);
if(f1 > maxx || f2 > maxy)
{
printf("0\n");
continue;
}
sx = (int) f1;
sy = (int) f2;
bfs();
printf("%d\n",dis[sx][sy]);
}
return ;
}
POJ2049Finding Nemo(bfs + 构图)的更多相关文章
- 【bfs+优先队列】POJ2049-Finding Nemo
基本上算是普通但略有些繁琐的广搜.给出的墙面和门的坐标为点,而Nemo位于方格中. [思路] 首先思考一下如何存储下整个坐标系.我们预先约定,用一个方格的左下角顶点坐标来作为这个方格的坐标.map[i ...
- POJ 2049 Finding Nemo bfs 建图很难。。
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6952 Accepted: 1584 Desc ...
- POJ:2049Finding Nemo(bfs+优先队列)
http://poj.org/problem?id=2049 Description Nemo is a naughty boy. One day he went into the deep sea ...
- Finding Nemo(bfs)
Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6988 Accepted: 1600 Description Nemo ...
- POJ 2049— Finding Nemo(三维BFS)10/200
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...
- bnuoj 25662 A Famous Grid (构图+BFS)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662 #include <iostream> #include <stdio.h ...
- poj 2049 Finding Nemo(优先队列+bfs)
题目:http://poj.org/problem?id=2049 题意: 有一个迷宫,在迷宫中有墙与门 有m道墙,每一道墙表示为(x,y,d,t)x,y表示墙的起始坐标d为0即向右t个单位,都是墙d ...
- ACM/ICPC 之 网络流-拆点构图(POJ2391)
需要直接到达,因此源点经过三条边后必须要达到汇点,但为了保证网络流的正确性(路径可反悔),因此不可限制层次网络的最高层次为3,最好的方法既是让所有点拆分成两个点,一个点从汇点进入,一个点通向汇点,任意 ...
- POJ3281 Dining(拆点构图 + 最大流)
题目链接 题意:有F种食物,D种饮料N头奶牛,只能吃某种食物和饮料(而且只能吃特定的一份) 一种食物被一头牛吃了之后,其余牛就不能吃了第一行有N,F,D三个整数接着2-N+1行代表第i头牛,前面两个整 ...
随机推荐
- 分布式监控系统Zabbix-3.0.3-完整安装记录(4)-解决zabbix监控图中出现中文乱码问题
之前部署了Zabbix-3.0.3监控系统,在安装数据库时已经将zabbix库设置了utf-8字符. 首先确定zabbix开启了中文支持功能:登录到zabbix服务器的数据目录下(前面部署的zabbi ...
- vuejs常用指令
v-model 表单双向绑定 v-for="文件名 in 向文件添加内容的文件名" 渲染列表(内容) 将中间的in可改写成of v-on:click="name" ...
- android实现点击背景图片不同区域实现不同事件
有时候我们拿到一张背景图片,客户要求点击图片的不同区域去跳转或者实现不同的操作事件.我们首先要确认图片的点击区域,往往我们会在布局文件那里下手,但是这样不好做适配,所以我实现了以下方法,基本功能可以实 ...
- C# is as
if(obj is ClassA) //遍历类层次,看OBJ是不是ClassA类型{ ClassA a=(ClassA) obj; //遍历类层次,看obj能否转换为ClassA,不成功则抛出异 ...
- [word]2010中插入公式自动编号并且公式不自动缩小/变小
要实现在word2010中插入公式自动编号,就要用到自动图文集功能,具体操作如下: 1.先制定制表位位置:单击一个空白段落,然后双击标尺线的底部:这会激活"制表位"对话框,如图所示 ...
- PLC梯形图编程练习
在PLC培训软件中完成如下两个练习,并把对应的梯形图程序发表在博客上. 交通灯控制 在如下图的场景中,打开SW1开关后,交通灯控制器开始工作,关闭SW1则控制器停止工作. SW2为控制模式选择开关: ...
- RESideMenu左右半侧滑的功能实现,主视图会和状态栏(StatusBar)不会随着一起滑动
具体demo去github下载,这里不详细描述
- 【WEB前端经验之谈】没有速成,只有不断积累。
2013年8月25日,我人生中的第一份正式工作开始了,第一份工作做的是当时学习的asp.net,用的是C#语言. 到第一家公司上班是公司是做一个OA系统,不过我去的时候大部分都已经完成了,剩下的都是细 ...
- jsonp的后台怎么返回去数据
- js设置输入框失去光标与光标选中时样式
输入框样式 <script language="javascript" type="text/javascript"> function glb_s ...