Finding Nemo(搜索)
http://poj.org/problem?id=2049
题意:有一个迷宫,迷宫中有墙、门和空地。有M道墙,每一道墙用(x,y,d,t)表示,(x,y)表示墙的起始坐标,(d=1,t)表示向上t个单位都是墙;(d=0,t)表示向右t个单位都是墙。
有N扇门,用(x,y,d)表示,(x,y)表示门的起始坐标,d=1,表示向上一个单位都是门;d=0,表示向右一个单位都是门。 给出Nemo的起始位置(f1,f2),问起点到(0,0)的最少要穿过的门。
表示对搜索的题很晕。。看到题不知道该怎么存,看了别人的题解才懂点。。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
const int INF=<<;
const int N=;
int dir[][]= {{-,},{,},{,-},{,}};
int xx[N][N],yy[N][N];
int dis[N][N];
int max_x,max_y;
int boundary(int x,int y)//边界判断
{
if(x> && x<=max_x && y<=max_y && y>)
return ;
return ;
}
int change(int x,int y,int d)//方向转换
{
if(d==) return yy[x-][y];
if(d==) return yy[x][y];
if(d==) return xx[x][y-];
return xx[x][y];
}
int bfs(int x,int y)
{
queue<int>q;
while(!q.empty()) q.pop();
for (int i = ; i <= max_y; i++)
{
for (int j = ; j <= max_x; j++)
dis[i][j] = INF;
}
dis[][]=;
q.push();
q.push();
while(!q.empty())
{
int x1=q.front();
q.pop();
int y1=q.front();
q.pop();
for (int i = ; i < ; i++)
{
int dx = x1+dir[i][];
int dy = y1+dir[i][];
int turn = change(x1,y1,i);
if(boundary(dx,dy) && dis[dx][dy]>dis[x1][y1]+turn)
{
dis[dx][dy]=dis[x1][y1]+turn;//更新最小步数
q.push(dx);
q.push(dy);
}
}
}
int ans = dis[x][y]==INF?-:dis[x][y];
return ans;
}
int main()
{
int n,m;
int x,y,d,l;
while(~scanf("%d%d",&n,&m))
{
if(n==-&&m==-) break;
max_x = max_y = -;
memset(xx,,sizeof(xx));
memset(yy,,sizeof(yy));
for (int i = ; i < n; i++)
{
scanf("%d%d%d%d",&x,&y,&d,&l);
if(d)
{
for (int j = ; j < l; j++)
yy[x][y+j+]=INF;
max_y=max(y+l+,max_y);
max_x=max(x+,max_x);
}
else
{
for (int j = ; j < l; j++)
{
xx[x+j+][y]=INF;
max_y=max(y+,max_y);
max_x=max(x+l+,max_x);
}
}
}
for (int i = ; i < m; i++)
{
scanf("%d%d%d",&x,&y,&d);
if(d) yy[x][y+]=;
else xx[x+][y]=;
}
double sx,sy;
int sx1,sy1;
scanf("%lf%lf",&sx,&sy);
sx1 = (int)sx+;
sy1 = (int)sy+;
if(!(sx>= && sx<= && sy>= && sy<=))//Nemo可能在迷宫外
printf("0\n");
else
printf("%d\n",bfs(sx1,sy1));
}
return ;
}
Finding Nemo(搜索)的更多相关文章
- 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 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 2049— Finding Nemo(三维BFS)10/200
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...
- Finding Nemo(bfs)
Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6988 Accepted: 1600 Description Nemo ...
- poj 2049 Finding Nemo(优先队列+bfs)
题目:http://poj.org/problem?id=2049 题意: 有一个迷宫,在迷宫中有墙与门 有m道墙,每一道墙表示为(x,y,d,t)x,y表示墙的起始坐标d为0即向右t个单位,都是墙d ...
- POJ2049Finding Nemo(bfs + 构图)
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 8456 Accepted: 1975 Desc ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- [zz]Lessons from Pixar: Why Software Developers Should Be Storytellers
http://firstround.com/article/lessons-from-pixar-why-software-developers-should-be-story-tellers Whe ...
随机推荐
- Android中ProgressDialog自动消失
主要函数部分代码: final ProgressDialog proDialog = android.app.ProgressDialog .show(MainActivity.this, " ...
- cc.Node—Action
1: Action类是动作命令,我们创建Action,然后节点运行action就能够执行Action的动作; 2: Action分为两类: (1) 瞬时就完成的ActionInstant, (2) 要 ...
- mysql外键是多个id组成的字符串,查询方法
借鉴:mysql使用instr达到in(字符串)的效果 结论:select * from 表名where INSTR(CONCAT(字符串),CONCAT(表id)) 问题来源:一表中的某字段是另一表 ...
- 如何使用微信小程序video组件播放视频
相信很多人都有在手机上看视频的习惯,比较看视频更真实更形象.那么我们在微信小程序中如何观看视频呢?这就需要video组件的帮忙了.今天我们就给大家演示一下,如何用微信小程序组件video播放视频.我们 ...
- 洛谷 2344 奶牛抗议 Generic Cow Protests, 2011 Feb
[题解] 我们可以轻松想到朴素的状态转移方程,但直接这样做是n^2的.所以我们考虑采用树状数组优化.写法跟求逆序对很相似,即对前缀和离散化之后开一个权值树状数组,每次f[i]+=query(sum[i ...
- angular环境安装与配置
1.安装npm和nodejs,下载地址:https://nodejs.org/en/download/ node -v npm -v 2.配置淘宝代理,下载node_modules npm con ...
- BZOJ 3993 Luogu P3324 [SDOI2015]星际战争 (最大流、二分答案)
字符串终于告一段落了! 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3993 (luogu) https://www.l ...
- POJ 2217 Secretary
Secretary Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ 3630
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20894 Accepted: 6532 Descripti ...
- 洛谷—— P1725 琪露诺
https://www.luogu.org/problem/show?pid=1725 题目描述 在幻想乡,琪露诺是以笨蛋闻名的冰之妖精.某一天,琪露诺又在玩速冻青蛙,就是用冰把青蛙瞬间冻起来.但是这 ...