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地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
随机推荐
- AngularJS学习笔记(一)走近AngularJS
什么是AngularJS AngularJS是一款优秀的前端JS框架,是Google多款产品之一,简称ng. ng有着诸多特性,最为核心的是:MVVM.模块化.自动化双向数据绑定.语义化标签.依赖注入 ...
- WCF 获取客户端IP
public class Service2 : IService2 { public User DoWork() { Console.WriteLine(ClientIpAndPort()); }; ...
- Linux分区方式及关闭iptables和selinux的方式
分区方式一般有三种 第一种:数据不是很重要 /boot(系统的引导分区): 系统引导的信息/软件 系统的内核 200M swap( 交换分区): 为了避免系统内存用光了导致系统 宕机 如果系统内存 ...
- 使用JavaScript实现ajax
实现ajax之前必须要创建一个 XMLHttpRequest 对象.如果不支持创建该对象的浏览器,则需要创建 ActiveXObject.具体方法如下: var xmlHttp; function ...
- input点击链接另一个页面,各种操作。
1.链接到某页<input type="button" name="Submit" value="确 定" class="b ...
- Shiro遇到的SecurityManager红色警告
问题如图 需要添加一个导入 import org.apache.shiro.mgt.SecurityManager; 这样就不会报错了
- Java虚拟机 - Javac编译与JIT编译
[深入Java虚拟机]之七:Javac编译与JIT编译 编译过程 不论是物理机还是虚拟机,大部分的程序代码从开始编译到最终转化成物理机的目标代码或虚拟机能执行的指令集之前,都会按照如下图所示的各个步骤 ...
- Linux常用基本命令[find]用法(1)
find是个很强大的命令,用法很多. 作用:查找目录下的文件,同时也可以调用其他命令执行相应的操作 用法: find [选项] [路径][操作语句] find [-H] [-L] [-P] [-D d ...
- linux系统编程:进程控制(fork)
在linux中,用fork来创建一个子进程,该函数有如下特点: 1)执行一次,返回2次,它在父进程中的返回值是子进程的 PID,在子进程中的返回值是 0.子进程想要获得父进程的 PID 需要调用 ge ...
- Codeforces445B(SummerTrainingDay06-N 并查集)
B. DZY Loves Chemistry time limit per test:1 second memory limit per test:256 megabytes input:standa ...