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. 04SpringMvc_映射器_BeanNameUrlHanderMapping

    这篇文章我们讲的是映射器,映射器的作用是什么样的请求交给Action,如果我们没有在xml配置文件中进行配置,默认的就是BeanNameUrlHanderMapping. 我们讲一个案例增加用户的案例 ...

  2. ztree插件(JQuery Tree)

    本次使用的ztree插件,基本上所有的需求都能满足,可谓功能强大. * [http://www.ztree.me/v3/api.php zTree v3.0 API 文档] * [http://www ...

  3. PATH路径出错导致任何命令都找不到解决方法

    1.export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin这样可以保证命令行命令暂时可以使用.命令执行完之后先不要关闭终端或者cd /usr/ ...

  4. 【转】【WPF】WPF 登录窗口关闭时打开主窗口

    在WPF中设计登录窗口关闭时打开主窗口,自动生成的App.xaml不能满足要求, 1.把App.xaml的属性窗口中的生成操作设定为 无 2.添加Program类 static class Progr ...

  5. Linux 网络编程三(socket代码详解)

    //网络编程客户端 #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  6. char 转wchar_t 及wchar_t转char

    利用WideCharToMultiByte函数来转换,该函数映射一个unicode字符串到一个多字节字符串.通常适合于window平台上使用. #include <tchar.h> #in ...

  7. Zxing二维码重复扫描,不退出。

    扫描条码,把手机实现类似超市扫描枪之类的连续扫描. private void continuePreview(){ SurfaceView surfaceView = (SurfaceView) fi ...

  8. REST: C#调用REST API (zz)

    由于辞职的原因,最近正在忙于找工作.在这段期间收到了一家公司的上机测试题,一共两道题,其中一道题是关于REST API的应用.虽然在面试时,我已经说过,不懂REST,但那面试PM还是给了一道这题让我做 ...

  9. 20145233《Java程序设计》课程总结

    20145233 <Java程序设计>学习总结 每周学习博客汇总 20145233韩昊辰 第一周总结 20145233韩昊辰 第二周总结 20145233韩昊辰 第三周总结 2014523 ...

  10. HoloLens开发手记 - Unity之Locatable camera 使用相机

    Enabling the capability for Photo Video Camera 启用相机能力 为了使用摄像头,我们必须启用WebCam能力. 在Unity中打开Player settin ...