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头牛,前面两个整 ...
随机推荐
-
Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解
/* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...
-
Android 三大图片加载框架的对比——ImageLoader,Picasso,Glide
一.ImageLaoder介绍 << Universal ImageLoader 是很早开源的图片缓存,在早期被很多应用使用 多线程下载图片,图片可以来源于网络,文件系统,项目文件夹ass ...
-
js获取url的常用方法
//设置或获取对象指定的文件名或路径. console.log(window.location.pathname) //设置或获取整个 URL 为字符串. console.log(window.loc ...
-
在html里添加视频的方法
在html里添加本地视频的方法: <!DOCTYPE HTML><html><body><video width="320" height ...
-
Matlab绘图高级部分
图形是呈现数据的一种直观方式,在用Matlab进行数据处理和计算后,我们一般都会以图形的形式将结果呈现出来.尤其在论文的撰写中,优雅的图形无疑会为文章加分.本篇文章非完全原创,我的工作就是把见到的Ma ...
-
Git使用过程
Git-------目前世界上最先进的分布式版本控制系统(没有之一) 什么是版本控制系统? 说简单点,就是一个文件,对其增加.删除.修改都可以被记录下来,不仅自己可以修改,其他人也可以进行修改 每次对 ...
-
configure mount nfs
qemu-img convert -f raw -O qcow2 nix.img ruiynix.qcow2 1,yum createrepo
-
Centos7搭建邮件服务器-Postfix+Cyrus-sasl+Courier-authlib+Dovecot+ExtMail+Centos7
1.环境介绍 MTA: Postfix 3.1.4 SASL: Cyrus-sasl 2.1.26 ; Courier-authlib 0.66.1(Cyrus-sasl使用Courier-authl ...
-
CSS实现树形结构 + js加载数据
看到一款树形结构,比较喜欢它的样式,就参照它的外观自己做了一个,练习一下CSS. 做出来的效果如下: li { position: relative; padding: 5px 0; margin:0 ...
-
git help 机器翻译
该篇发布仅为博主个人保存并参考,内容可能不对 usage: git [--version] [--help] [-C <path>] [-c <name>=<value& ...