版权声明:本文为博主原创文章,未经博主同意不得转载。 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. ASP.NET开发,从二层至三层,至面向对象 (3)

    继续上一篇<ASP.NET开发,从二层至三层,至面向对象 (2)>http://www.cnblogs.com/insus/p/3825805.html .我们学会了怎样创建对象,把Biz ...

  2. Java并发编程-Executor框架集

    Executor框架集对线程调度进行了封装,将任务提交和任务执行解耦. 它提供了线程生命周期调度的所有方法,大大简化了线程调度和同步的门槛. Executor框架集的核心类图如下: 从上往下,可以很清 ...

  3. Android - AMS源码分析

    Android核心分析之AMS App和AMS(SystemServer进程)还有zygote进程分属于三个独立的进程 App与AMS通过Binder进行IPC通信,AMS(SystemServer进 ...

  4. Android - Dagger2 使用和原理

    Dagger2从入门到放弃再到恍然大悟 http://www.jianshu.com/p/cd2c1c9f68d4 http://www.jianshu.com/p/39d1df6c877d http ...

  5. org.springframework.transaction.CannotCreateTransactionException Could not open

    org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for ...

  6. CSS布局之——对齐方式

    一.水平居中: (1). 行内元素的水平居中? 如果被设置元素为文本.图片等行内元素时,在父元素中设置text-align:center实现行内元素水平居中,将子元素的display设置为inline ...

  7. 免费的局域网协作办公方式—onlyoffice文档协作

    局域网内想享受协作办公的乐趣,请移步到这里按照步骤部署.https://blog.csdn.net/hotqin888/article/details/79337881 它是免费开源的,经过作者的一些 ...

  8. OpenCV 线条及形状

    1.线条 # dst 相当于画板 dst=np.zeros((300,300,3),np.uint8) # #参1 图像 参2 起始点 参3 结束点 参4 颜色 line=cv2.line(dst,( ...

  9. SQL Server存储过程输入参数使用表值

    转载自:http://blog.csdn.net/smithliu328/article/details/9996149 在2008之前如果我们想要将表作为输入参数传递给SQL Server存储过程使 ...

  10. Oracle EBS AP取消核销

    --取消核销 created by jenrry 20170425 DECLARE l_result BOOLEAN; l_msg_count NUMBER; l_result_n varchar2( ...