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(搜索)的更多相关文章

  1. Finding Nemo 分类: POJ 2015-07-11 10:11 10人阅读 评论(0) 收藏

    Finding Nemo Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 8117   Accepted: 1883 Desc ...

  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 2049— Finding Nemo(三维BFS)10/200

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...

  5. Finding Nemo(bfs)

    Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 6988   Accepted: 1600 Description Nemo ...

  6. poj 2049 Finding Nemo(优先队列+bfs)

    题目:http://poj.org/problem?id=2049 题意: 有一个迷宫,在迷宫中有墙与门 有m道墙,每一道墙表示为(x,y,d,t)x,y表示墙的起始坐标d为0即向右t个单位,都是墙d ...

  7. POJ2049Finding Nemo(bfs + 构图)

    Finding Nemo Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 8456   Accepted: 1975 Desc ...

  8. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  9. [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 ...

随机推荐

  1. JS中遍历EL表达式中后台传过来的Java集合

    前言:在我的项目里有这么一个情况,后台直接model.addAttribute()存储了一个对象,此对象内部有一个集合,前端JSP处理的方法正常情况下就是直接使用EL表达式即可.但是如果在JS中需要使 ...

  2. Pycharm Anaconda 安装dlib

    由于采用python3.7安装会出现各种问题,两种解决方法. 1)安装Cmake boost等(不推荐,麻烦且不容易成功). 2)安装Anaconda,创建一个python3.6的环境. 这里使用第二 ...

  3. XML中的特殊(保留)字符数据

    XML中的特殊(保留)字符数据 制作人:全心全意 在XML文档中,有些字符会被XML解析器当作标记进行处理.如果希望把这些字符作为普通字符处理,就需要使用实体引用或CDATA段. 使用实体引用 为了避 ...

  4. 常量Constant

    常量通常指的是一个固定的值,例如:1.2.3.’a’.’b’.true.false.”helloWorld”等. 在Java语言中,主要是利用关键字final来定义一个常量. 常量一旦被初始化后不能再 ...

  5. android Fragment用法

    Fragment常用的三个类:android.app.Fragment 主要用于定义Fragmentandroid.app.FragmentManager 主要用于在Activity中操作Fragme ...

  6. T1080 线段树练习 codevs

    http://codevs.cn/problem/1080/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 一行N个方 ...

  7. vue2源码浏览分析01

    1.构造函数  Vue$3 function Vue$3 (options) { if ("development" !== 'production' && !(t ...

  8. docker: 解决centos7下cgroup.procs: no such device的错误

    在centos7下,运行docker run的时候会发生cgroup.procs: no such device的错误,解决方法是编辑 /lib/systemd/system/docker.servi ...

  9. OpenJudge百炼习题解答(C++)--题2704:竞赛评分

    题: 总时间限制:  1000ms  内存限制:  65536kB 描写叙述 现举行一次小竞赛,參赛的3支队伍,编号为1,2,3.每支队列轮流回答问题,假设回答正确,加10分;回答错误,扣10分;放弃 ...

  10. struts2前端页面读取Clob/BLOB

    在通过Struts2标签显示对象的Clob属性值的时候.显示的并非CLOB或者BLOB的内容,而是显示的toString方法的值 比如我在实体中的注解为: @Lob @Column(name = &q ...