POJ 2049— Finding Nemo(三维BFS)10/200
版权声明:本文为博主原创文章,未经博主同意不得转载。 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
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 7410 | Accepted: 1725 |
Description
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
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
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的更多相关文章
- poj 2049 Finding Nemo(优先队列+bfs)
题目:http://poj.org/problem?id=2049 题意: 有一个迷宫,在迷宫中有墙与门 有m道墙,每一道墙表示为(x,y,d,t)x,y表示墙的起始坐标d为0即向右t个单位,都是墙d ...
- POJ 2049 Finding Nemo bfs 建图很难。。
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6952 Accepted: 1584 Desc ...
- POJ 2049 Finding Nemo
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 8631 Accepted: 2019 Desc ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- Finding Nemo(bfs)
Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6988 Accepted: 1600 Description Nemo ...
- POJ——2251Dungeon Master(三维BFS)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25379 Accepted: 9856 D ...
- Finding Nemo 分类: POJ 2015-07-11 10:11 10人阅读 评论(0) 收藏
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 8117 Accepted: 1883 Desc ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
随机推荐
- [日常] Go语言圣经-Panic异常,Recover捕获异常习题
Go语言圣经-Panic异常1.当panic异常发生时,程序会中断运行,并立即执行在该goroutine中被延迟的函数(defer 机制)2.不是所有的panic异常都来自运行时,直接调用内置的pan ...
- oracle的学习笔记(转)
Oracle的介绍 1. Oracle的创始人----拉里•埃里森 2. oracle的安装 [连接Oracle步骤](](https://img2018.cnblogs.com/blog/12245 ...
- Humble Numbers(hdu1058)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Java - TreeSet源码解析
Java提高篇(二八)------TreeSet 与HashSet是基于HashMap实现一样,TreeSet同样是基于TreeMap实现的.在<Java提高篇(二七)-----TreeMap& ...
- Java - “JUC”锁
[Java并发编程实战]-----“J.U.C”:锁,lock 在java中有两种方法实现锁机制,一种是在前一篇博客中([java7并发编程实战]-----线程同步机制:synchronized) ...
- HDU4289(KB11-I 最小割)
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- python学习之老男孩python全栈第九期_day002作业
1. 判断下列逻辑语句的True,False.(1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6Tru ...
- KCF跟踪算法 入门详解
一.算法介绍 KCF全称为Kernel Correlation Filter 核相关滤波算法.是在2014年由Joao F. Henriques, Rui Caseiro, Pedro Martins ...
- Linux CentOS7下svn+tomcat9.0+maven3.3+jenkins实现web项目自动构建与远程发布
CentOS7下svn+tomcat9.0+maven3.3+jenkins实现web项目自动构建与远程发布 by:授客 QQ:1033553122 目录 一. 实践环境. 1 二. 安装 ...
- [Android] 状态栏的一些认识
前段时间遇到几个关于状态栏的问题,又了解了一下状态栏相关的知识,现在做一下记录. 本文地址:http://www.cnblogs.com/rossoneri/p/4316343.html 前戏和问题 ...