POJ 2049 Finding Nemo
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 8631 | Accepted: 2019 |
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
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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int inf=0x3f3f3f3f;
struct node {
int x, y;
int ans;
} f1,f2;
int map[][][];
int vis[][];
int jx[]= {,,,-};
int jy[]= {,-,,};
int min1;
void bfs(int s, int e)
{
int i, j;
queue<node>q;
f1.x=s;
f1.y=e;
f1.ans=;
vis[s][e]=;
q.push(f1);
min1=inf;
while(!q.empty()) {
f1=q.front();
q.pop();
if(f1.x<=||f1.x>=||f1.y<=||f1.y>=) {//边界处理,但是不能跳出,只能跳过,因为存在多个门的情况
min1=min(min1,f1.ans); //走出去之后,保存最小通过门数
continue ;
}
for(i=; i<; i++) {
f2.x=f1.x+jx[i];
f2.y=f1.y+jy[i];
if(i==) {//向上走
if(!vis[f2.x][f2.y]&&map[f1.x][f1.y][]!=) {
if(map[f1.x][f1.y][]==)
f2.ans=f1.ans+;
else
f2.ans=f1.ans;
vis[f2.x][f2.y]=;
q.push(f2);
}
} else if(i==) {//向下走
if(!vis[f2.x][f2.y]&&map[f2.x][f2.y][]!=) {
if(map[f2.x][f2.y][]==)
f2.ans=f1.ans+;
else
f2.ans=f1.ans;
vis[f2.x][f2.y]=;
q.push(f2);
}
} else if(i==) {//向右走
if(!vis[f2.x][f2.y]&&map[f1.x][f1.y][]!=) {
if(map[f1.x][f1.y][]==)
f2.ans=f1.ans+;
else
f2.ans=f1.ans;
vis[f2.x][f2.y]=;
q.push(f2);
}
} else if(i==) {//向左走
if(!vis[f2.x][f2.y]&&map[f2.x][f2.y][]!=) {
if(map[f2.x][f2.y][]==)
f2.ans=f1.ans+;
else
f2.ans=f1.ans;
vis[f2.x][f2.y]=;
q.push(f2);
}
}
}
}
}
int main()
{
int n, m, i, j;
int x,y,d,t;
double a1,a2;
int int_x,int_y;
while(scanf("%d %d",&m,&n)!=EOF) {
if(n==-&&m==-) break;
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
for(i=; i<m; i++) {
scanf("%d %d %d %d",&x, &y, &d, &t);
if(d) {
for(j=; j<t; j++) {
map[x-][y+j][]=;
}
} else {
for(j=; j<t; j++) {
map[x+j][y-][]=;
}
}
}
for(i=; i<n; i++) {
scanf("%d %d %d",&x,&y,&d);
if(d) {
map[x-][y][]=;
} else {
map[x][y-][]=;
}
}
scanf("%lf %lf",&a1,&a2);
int_x=a1;
int_y=a2;
if(int_x<=||int_x>=||int_y<=||int_y>=) {//后台可能出现 0,0 200+,200+,虽然不符合题目描述
printf("0\n");
continue ;
}
bfs(int_x,int_y);
if(min1==inf)
printf("-1\n");
else
printf("%d\n",min1);
}
return ;
}
POJ 2049 Finding Nemo的更多相关文章
- POJ 2049— Finding Nemo(三维BFS)10/200
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...
- POJ 2049 Finding Nemo bfs 建图很难。。
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6952 Accepted: 1584 Desc ...
- poj 2049 Finding Nemo(优先队列+bfs)
题目:http://poj.org/problem?id=2049 题意: 有一个迷宫,在迷宫中有墙与门 有m道墙,每一道墙表示为(x,y,d,t)x,y表示墙的起始坐标d为0即向右t个单位,都是墙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 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
- Finding Nemo(搜索)
http://poj.org/problem?id=2049 题意:有一个迷宫,迷宫中有墙.门和空地.有M道墙,每一道墙用(x,y,d,t)表示,(x,y)表示墙的起始坐标,(d=1,t)表示向上t个 ...
- poj 3376 Finding Palindromes
Finding Palindromes http://poj.org/problem?id=3376 Time Limit: 10000MS Memory Limit: 262144K ...
- Finding Nemo(bfs)
Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6988 Accepted: 1600 Description Nemo ...
- POJ 3376 Finding Palindromes(扩展kmp+trie)
题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...
随机推荐
- PHP之验证码识别
首先推荐几篇有关验证码识别的文章,觉得不错 php实现验证码的识别(初级篇) 关于bp神经网格识别验证码 一.思路 碰见一个验证码,如果我们想要识别它,我们需要的是做什么呢? 我们先观察几个验证码.. ...
- sqlserver cte递归向上统计
数据字典如下
- DDD的ABP开发框架
基于DDD的ABP开发框架初探 一.基本概念 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ABP是土耳其的以为架构师hikalkan开发 ...
- linux中VI编辑器使用个人记录
VI编辑器有三种编辑模式:命令模式.最后行模式.文本编辑模式 启动VI后进入的第一种模式是”命令模式“.从命令模式可进入最后行模式和编辑模式.而后两种模式之间不能直接切换.必须按ESC键退回到命令模式 ...
- GNU FORK PTHREAD SIGNALS
Linux程序设计入门 - fork, pthread, and signals 在UNIX程序设计中,学会fork及signal的运用,算是相当基本的功夫. fork()及signal经常运用在da ...
- [BZOJ 1816] [Cqoi2010] 扑克牌 【二分答案】
题目链接:BZOJ - 1816 题目分析 答案具有可以二分的性质,所以可以二分答案. 验证一个答案 x 是否可行,就累加一下各种牌相对于 x 还缺少的量,如果总和超过了 x 或 m ,就不可行. 因 ...
- [BZOJ 1026] [SCOI 2009] Windy数 【数位DP】
题目链接:BZOJ - 1026 题目分析 这道题是一道数位DP的基础题,对于完全不会数位DP的我来说也是难题.. 对于询问 [a,b] 的区间的答案,我们对询问进行差分,求 [0,b] - [0,a ...
- hdu 5063 Operation the Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5063 思路:因为3查询最多50,所以可以在查询的时候逆操作找到原来的位置,然后再求查询的值. #include ...
- index unique scan
INDEX UNIQUE SCAN 索引唯一扫描.单块读 只可能发生在unique index/primary key 等值查找 等待事件:db file s ...
- AlgorithmsI Programming Assignment 1: PercolationStats.java
import edu.princeton.cs.algs4.StdOut; import edu.princeton.cs.algs4.StdRandom; import edu.princeton. ...