版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/u013497151/article/details/29562915

海底总动员。。。。

这个题開始不会建图,彻底颠覆曾经我对广搜题的想法。想了好久。 忽然想到省赛时HYPO让我做三维BFS来着,一直没做,看到POJ计划这个题。就是三维BFS解题,就做了一下, 对于这个题。。

实在不知道说什么好,又坑、又SB,POJ的后台数据和题目描写叙述的全然不一样,看了DIscuss之后開始 修改代码,最后改的又臭又长,搜了无数题解找数据,卡了整整两天。

挥挥洒洒 160行。

。。。同一时候也是我第一次使用  三维建图+BFS,纪念一下!

2049 算是我攻克POJ计划的第一个卡两天的难关。POJ完毕进度  10/200,为了庆贺一下。我决定回去睡个觉;

input

0 0
-100.0 -100.0

-1 -1

打印

0

Finding Nemo
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 7410   Accepted: 1725

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

Source

ME             time          WA     RE     AC

1536KB      32ms         15   +   9   =  1 

第三维储存 墙和门

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#define right 0
#define up 1
#define wall 3
#define door 4
#define null 0
const int N = 210;
const int Size = 999999;
const int INF = 1<<20;
using namespace std;
struct node{
int x,y,ans;
}q[Size];
int mapp[N][N][2];
int vis[N][N];
int n,m,result;
int mv[4][2] = {{1,0},{0,-1},{0,1},{-1,0}}; void init()
{
result = 0;
memset(mapp,0,sizeof(mapp));//初始化空地
memset(vis,0,sizeof(vis));
}
void BFS(int x,int y)
{
node f,t;
int s = 0,e = 0;
f.x = x;
f.y = y;
f.ans = 0;
q[e++] = f;
vis[x][y] = 1;
result = INF;
while(s < e)
{
t = q[s++];
if(t.x ==0 || t.y ==0 || t.x > 198 || t.y > 198 ) //边界处理,可是不能跳出。仅仅能跳过,由于存在多个门的情况
{
if(result > t.ans) //走出去之后。保存最小通过门数
result = t.ans;
continue; //注意,不要break。
} for(int i = 0;i<4;i++)
{
f.x = t.x + mv[i][0];
f.y = t.y + mv[i][1];
// printf("asd\n");
if(i==0)
{
if(!vis[f.x][f.y] && mapp[t.x][t.y][up]!=wall)//右
{
if(mapp[t.x][t.y][up]==door)
f.ans = t.ans +1;
else
f.ans = t.ans;
vis[f.x][f.y] = 1;
q[e++] = f;
}
}
else if(i==1)
{
if(!vis[f.x][f.y] && mapp[f.x][f.y][right]!=wall) //下
{
if(mapp[f.x][f.y][right]==door)
f.ans = t.ans +1;
else
f.ans = t.ans;
vis[f.x][f.y] = 1;
q[e++] = f;
}
}
else if(i==2)
{
if(!vis[f.x][f.y] && mapp[t.x][t.y][right]!=wall)//上
{
if(mapp[t.x][t.y][right]==door)
f.ans = t.ans +1;
else
f.ans = t.ans;
vis[f.x][f.y] = 1;
q[e++] = f;
}
}
else if(i==3)
{
if(!vis[f.x][f.y] && mapp[f.x][f.y][up]!=wall)//左
{
if(mapp[f.x][f.y][up]==door)
f.ans = t.ans +1;
else
f.ans = t.ans;
vis[f.x][f.y] = 1;
q[e++] = f;
}
}
}
}
} int main()
{
int x,y,d,t;
double Nemo_x,Nemo_y;
while(~scanf("%d%d",&m,&n))
{
if(n==-1 && m==-1)
break;
init();
for(int num_wall = 0;num_wall < m;num_wall++)
{
scanf("%d%d%d%d",&x,&y,&d,&t);
if(d)
{
for(int num = 0;num < t;num++)
mapp[x-1][y+num][up] = wall;
}
else
{
for(int num = 0;num < t;num++)
mapp[x+num][y-1][right] = wall;
}
}
for(int num_door = 0;num_door < n;num_door++)
{
scanf("%d%d%d",&x,&y,&d);
if(d)
mapp[x-1][y][up] = door;
else
mapp[x][y-1][right] = door;
}
int xx,yy;
cin>>Nemo_x>>Nemo_y;
xx = (int)(Nemo_x + 0.0001);//这里要注意 double 取整时 会有损失0.99999 会变成0
yy = (int)(Nemo_y + 0.0001);
if(n==0 && m==0)
{
printf("0\n");
continue;
}
if(xx <=0 || yy <= 0 || xx >= 199 || yy >= 199) //后台可能出现 0,0 200+,200+。尽管不符合题目描写叙述
{
printf("0\n"); }
else
{
BFS(xx,yy);
(result==INF)?printf("-1\n"):printf("%d\n",result);
}
}
return 0;
}

POJ 2049— Finding Nemo(三维BFS)10/200的更多相关文章

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

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

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

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

  3. POJ 2049 Finding Nemo

    Finding Nemo Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 8631   Accepted: 2019 Desc ...

  4. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  5. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  6. Finding Nemo(bfs)

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

  7. POJ——2251Dungeon Master(三维BFS)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25379   Accepted: 9856 D ...

  8. Finding Nemo 分类: POJ 2015-07-11 10:11 10人阅读 评论(0) 收藏

    Finding Nemo Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 8117   Accepted: 1883 Desc ...

  9. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

随机推荐

  1. CSS学习笔记06 简单理解line-height

    在制作页面的时候,经常会遇到文本图片需要居中的情况,这时候,只要设置下文本的line-height属性等于包裹该文本的元素的高度即可让文本居中显示了,先来看看这个现象. <!DOCTYPE ht ...

  2. weex stream 之fetch的get、post获取Json数据

    无论何种平台,网络数据的获取都是十分重要的,最近学习weex,不可避免的要学习weex的数据请求方法了.网址 个人感觉,weex stream相较于其他平台,还算比较简单了,但是由于文档以及官方代码中 ...

  3. IntelliJ IDEA 2016.2 注册破解激活教程

    下载了IntelliJ IDEA 尽然需要激活,整了终于找到解决的办法了,记录下来. IntelliJ IDEA 2016.2下载地址:http://www.jetbrains.com/idea/do ...

  4. Java基础教程(23)--lambda表达式

    一.初识lambda表达式 1.定义   lambda表达式是一个可传递的代码块,或者更确切地说,可以把lambda表达式理解为简洁地表示可传递的匿名方法的一种方式.它没有名称,但它有参数列表.函数主 ...

  5. 【linux】suse linux 常用命令

    命令ls——列出文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当前目录下以字母a开头的所有文件 ls -l *.doc 给出当前目录下以.doc ...

  6. Application作用域实现:当用户重复登录时,挤掉原来的用户

    Application作用域实现:当用户重复登录时,挤掉原来的用户 一.实现思想 1.application(ServletContext)是保存在服务器端的作用域,我们在application中保存 ...

  7. 华中农业大学第五届程序设计大赛网络同步赛-K

    K.Deadline There are N bugs to be repaired and some engineers whose abilities are roughly equal. And ...

  8. 【 js 基础 】【 源码学习 】 深浅拷贝

    underscore的源码中,有很多地方用到了 Array.prototype.slice() 方法,但是并没有传参,实际上只是为了返回数组的副本,例如 underscore 中 clone 的方法: ...

  9. NOI.AC NOIP2018 全国热身赛 第四场

    心路历程 预计得分:\(0 + 100 +100\) 实际得分:\(10 + 100 + 0\) 神TM T3模数为啥是\(1e9 + 9\)啊啊啊啊,而且我也确实是眼瞎...真是血的教训啊.. T2 ...

  10. js-ES6学习笔记-Reflect

    1.Reflect对象与Proxy对象一样,也是 ES6 为了操作对象而提供的新 API.Reflect对象的设计目的有这样几个. 将Object对象的一些明显属于语言内部的方法(比如Object.d ...