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 ...
随机推荐
- How To:分析ORACLE监听日志中的IP信息
有时候需要分析出ORACLE日志监听中的IP信息,分享一个组合命令,Linux的shell下运行正常. grep "HOST=.*establish.*\* 0" listener ...
- CF450B Jzzhu and Sequences(矩阵加速)
CF450B Jzzhu and Sequences 大佬留言:这.这.不就是矩乘的模板吗,切掉它!! You are given xx and yy , please calculate $f_{n ...
- poj - 3254 - Corn Fields (状态压缩)
poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...
- trie字典树模板浅析
什么是trie? 百度百科 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的 ...
- Ubuntu---vim配置
1. Linux g++开启C++11支持 1.1 使用vim打开.bashrc文件 sudo vim ~/.bashrc 1.2 在some more ls aliases注释块的地方添加: ali ...
- TestNG安装及配置
1. 在idea中新建一个maven项目 2. 在pom.xml中添加testng和reportng依赖 <dependencies> <!-- 添加testNG依赖 --> ...
- LINUX-YUM 软件包升级器 - (Fedora, RedHat及类似系统)
yum install package_name 下载并安装一个rpm包 yum localinstall package_name.rpm 将安装一个rpm包,使用你自己的软件仓库为你解决所有依赖关 ...
- BZOJ 1370 团伙
两个认识的人不是朋友就是敌人,且满足: 1,朋友的朋友是朋友: 2,敌人的敌人是朋友. 一群朋友组成一个团伙,给出m个信息,求有多少个团伙. 用并查集,把一个点x拆成x和x’ 若a与b为朋友,则将a与 ...
- 百练2815:城堡问题(DFS)
描述 1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#---#####---# 2 # # ...
- 【12】AngularJS 事件
AngularJS 事件 AngularJS 有自己的 HTML 事件指令. ng-click 指令 ng-click 指令定义了 AngularJS 点击事件. <div ng-app=&qu ...