版权声明:本文为博主原创文章,未经博主同意不得转载。 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. MVC会员注销功能Cookie的应用

    我们实现了<MVC应用程序实现会员登录功能>http://www.cnblogs.com/insus/p/3466512.html 有登录就会有注销功能.此次Insus.NET练习一个MV ...

  2. Jquery停止动画

    stop方法 第一个参数:是否清除动画队列 true | false 第二个参数:是否跳转到动画最终效果   true | false 使用stop()方法的技巧 当下拉菜单和手风琴产生动画队列的问题 ...

  3. winform窗体 小程序【移动窗体和阴影】

    窗体无边框设置后无法移动,引用API 使其获得功能 移动 //窗体移动API [DllImport("user32.dll")] public static extern bool ...

  4. SQL Server T—SQL 表连接

    一  笛卡尔积 select  *  from  表1,表2 将两表的记录遍历显示 二表的横向连接 1   使用外键关系作为条件 select  *  from   表1,表2  where   表1 ...

  5. net 自定义泛型那点事

    泛型概述 泛型是程序设计语言的一种特性.允许程序员在强类型程序设计语言中编写代码时定义一些可变部分,那些部分在使用前必须作出指明.各种程序设计语言和其编译器.运行环境对泛型的支持均不一样.将类型参数化 ...

  6. koajs项目之memcached实现session共享

    在做nodejs服务的负载时要考虑到session共享的问题,一般常用的就是memcached方式实现的,本文主要介绍通过npm社区的几个模块轻松实现这个功能. 做koa的session一般会想到用k ...

  7. Spring学习笔记(一):快速入门

    Spring简介:        Spring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完 ...

  8. 最近见到一个用react native实现的标尺动画,不知道如何实现 帖两张图(新知食App)

    很恶心,这款App的标尺没有做兼容,我在模拟上看不到效果,无法说明我想做出的东西的效果,无奈粘一张图吧! 就是这么一个屌样子,如何实现?

  9. 关于动态加载js

    已知一个需要动态加载的js的文件路径数组,动态加载数组里面所有的js文件. 加载分两种情况: 1. 并行加载,不管js的执行顺序. 2. 串行加载,即一个一个加载,上一个加载完再加载下一个,直到最后. ...

  10. Prometheus Node_exporter 详解

    Basic CPU / Mem / Disk Info https://www.cnblogs.com/qianyuliang/p/10479515.html Basic CPU / Mem / Di ...