Finding Nemo
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 8456   Accepted: 1975

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 题意:先给出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 + 构图)的更多相关文章

  1. 【bfs+优先队列】POJ2049-Finding Nemo

    基本上算是普通但略有些繁琐的广搜.给出的墙面和门的坐标为点,而Nemo位于方格中. [思路] 首先思考一下如何存储下整个坐标系.我们预先约定,用一个方格的左下角顶点坐标来作为这个方格的坐标.map[i ...

  2. POJ 2049 Finding Nemo bfs 建图很难。。

    Finding Nemo Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 6952   Accepted: 1584 Desc ...

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

    http://poj.org/problem?id=2049 Description Nemo is a naughty boy. One day he went into the deep sea ...

  4. Finding Nemo(bfs)

    Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 6988   Accepted: 1600 Description Nemo ...

  5. POJ 2049— Finding Nemo(三维BFS)10/200

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...

  6. bnuoj 25662 A Famous Grid (构图+BFS)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662 #include <iostream> #include <stdio.h ...

  7. poj 2049 Finding Nemo(优先队列+bfs)

    题目:http://poj.org/problem?id=2049 题意: 有一个迷宫,在迷宫中有墙与门 有m道墙,每一道墙表示为(x,y,d,t)x,y表示墙的起始坐标d为0即向右t个单位,都是墙d ...

  8. ACM/ICPC 之 网络流-拆点构图(POJ2391)

    需要直接到达,因此源点经过三条边后必须要达到汇点,但为了保证网络流的正确性(路径可反悔),因此不可限制层次网络的最高层次为3,最好的方法既是让所有点拆分成两个点,一个点从汇点进入,一个点通向汇点,任意 ...

  9. POJ3281 Dining(拆点构图 + 最大流)

    题目链接 题意:有F种食物,D种饮料N头奶牛,只能吃某种食物和饮料(而且只能吃特定的一份) 一种食物被一头牛吃了之后,其余牛就不能吃了第一行有N,F,D三个整数接着2-N+1行代表第i头牛,前面两个整 ...

随机推荐

  1. 限制站点目录防止跨站的三种方案(使用open_basedir)

    nginx结合php的时候,可以使用open_basedir限制站点目录防止跨站具体实现方法有以下三种:注意:以下三种设置方法均需要PHP版本为5.3或者以上. 方法1)在Nginx配置文件中加入fa ...

  2. C# 无边框窗体之窗体移动

    点击窗体任意位置移动窗体: 需要添加命名空间: using System.Runtime.InteropServices; private const int WM_NCLBUTTONDOWN = 0 ...

  3. Linux下Tomcat的启动、关闭、杀死进程

    打开终端 cd /java/tomcat #执行 bin/startup.sh #启动tomcat bin/shutdown.sh #停止tomcat tail -f logs/catalina.ou ...

  4. [转]Ubuntu 用vsftpd 配置FTP服务器

    FROM : http://www.cnblogs.com/CSGrandeur/p/3754126.html 网上的文章好难懂啊..只想要简单粗暴,弄好能用就行啊,复杂的以后研究不行吗...折腾好久 ...

  5. [转]reids客户端 redis-cli用法

    连接:redis-cli -h machine -p port -n db转的:每次都搜,还是扔在这 Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以 ...

  6. C语言 百炼成钢9

    //题目25:求1+2!+3!+...+20!的和 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib ...

  7. 储存与更新 access_token

    做微信的项目,一开始就是 access_token 的申请,微信文档上写的比较清楚: 1.为了保密appsecrect,第三方需要一个access_token获取和刷新的中控服务器.而其他业务逻辑服务 ...

  8. c++中二进制和整数转化

    #1,包含文件 #include<bitset> #2,整数转化成二进制 int a = 63; bitset<6> bs(a); #3,二进制转化成整数 int b = bs ...

  9. [iOS翻译]《iOS7 by Tutorials》系列:iOS7的设计精髓(上)

    简介: 本文翻译自<iOS7 by Tutorials>一书的第一章“Designing for iOS 7”,主要从程序员角度介绍了iOS7的新设计理念,堪称神作!本文翻译仅作学习交流之 ...

  10. JS案例之7——瀑布流布局(2)

    这个例子与上一篇类似,唯一的区别是排序的方式有差别.上一篇是在高度最小的列里插入内容,这个案例是按顺序放置内容. 两种方法各有优缺点.第一种需要在图片内容加载完成的情况下有效.这个例子不需要在wind ...