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. onresize方法

    resize()方法可以写在当前页面包含的所有js里

  2. RDLC使用手册_RDLC报表部署

    原文:http://blog.csdn.net/lwjnumber/article/details/6590545 9.  RDLC报表部署(限于rdlc报表 windows应用程序) 1)    R ...

  3. AndroidStudio .gitinore编写

    # Built application files *.apk *.ap_ # files for the dex VM *.dex # Java class files *.class */R.ja ...

  4. Linux 发送信号

    使用kill命令 --在命令行执行kill命令.向指定进程发送信号. 使用kill函数 int kill(pid_t pid,int sig); --参数pid指定一个要杀死的进程,而sig是要发送的 ...

  5. matlab中textread

    今天打算跑下程序,突然发现,真的很烂,不会读入数据,简单的Iris.txt一上午都没读进去,在此对matlab中的textread函数做下总结,textscan函数待续. 本文主要内容引自http:/ ...

  6. livewriter写Blog 神秘失踪?

    现在习惯用livewriter来总结/记录一些知识并发布为Blog 与同行交流,但是今天发生了一个怪事,上午我整理了两篇文档当时就用livewriter发送到了Blog上,但是晚上来看的时候之前发送的 ...

  7. brew-cask 之本地更新 node

    本文同步自我的个人博客:http://www.52cik.com/2015/11/04/brew-cask-local.html 今天 Node v4.2.2 (LTS) 发布,什么是 LTS 呢,百 ...

  8. 在 Area 中使用RouteAttribute 定义路由, 并支持多语言

    业务上的一个需求, 同一页面, 两种不同的使用方法, 为了区分这两种需求, 需要加一个参数到 URL 中,不改路由的话, 是这样: http://localhost:16269/en-US/Forwa ...

  9. Java并发编程-ConcurrentHashMap

    特点: 将桶分段,并在某个段上加锁,提高并发能力 源码分析: V put(K key, int hash, V value, boolean onlyIfAbsent) { lock(); try { ...

  10. js判断页面点击事件

    <input type="submit" name="sb1" id="sb1" onclick="queryclick() ...