逃离迷宫 HDU1728 (bfs)
和连连看非常相似 都是求转向的BFS
改了一下就上交了。。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std; char world[][];
int f[][];
int dx[]={,,-,};
int dy[]={,,,-};
int n,m;int sx,sy,ex,ey;
int k; struct node
{
int x,y,d,chance;
node(int x=,int y=,int d=,int chance=):x(x),y(y),d(d),chance(chance){} }; void bfs()
{
memset(f,,sizeof(f)); node u(sx,sy,-,);
queue<node>q;
q.push(u);
while(!q.empty())
{
node u=q.front();q.pop();
if(u.x==ex&&u.y==ey&&u.chance<=k){printf("yes\n");return ;} for(int i=;i<;i++)
{ node v(u.x+dx[i],u.y+dy[i],u.d,u.chance); if(v.x>=&&v.x<=n&&v.y>=&&v.y<=m&&world[v.x][v.y]=='.')//这里(v.x==ex&&v.y==ey)不能改成与目标内容相同!!!
{
if(v.d!=-)
{
if(v.d!=i)
{
v.chance++;v.d=i;
} }
else v.d=i;
if(v.chance>k)continue;
if(v.chance<=f[v.x][v.y])
{ f[v.x][v.y]=v.chance;
q.push(v);
}
} } }
printf("no\n"); } int main()
{
int cas;cin>>cas;
while(cas--)
{
cin>>n>>m;
for(int i=;i<=n;i++)
{
scanf("%s",world[i]+);
} cin>>k>>sy>>sx>>ey>>ex; bfs(); } return ;
}
回顾:
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1000+5
#define inf 0x3f3f3f3f
int dx[]={,,,-};
int dy[]={,-,,};
int sx,sy,ex,ey,k;
int n,m;
char mp[N][N];
int vis[N][N]; bool inmap(int x,int y)
{
return x>=&&x<=n&&y>=&&y<=m;
} struct node
{
int x,y;
int chance;int dic;
node(int x,int y,int chance,int dic):x(x),y(y),chance(chance),dic(dic){}
}; void bfs()
{
queue<node>q;
node u(sx,sy,,-);
q.push(u);
memset(vis,inf,sizeof vis); while(!q.empty())
{
node u=q.front();q.pop();
if(u.x==ex&&u.y==ey){printf("yes\n");return ;} for(int i=;i<;i++)
{
node v=u;
v.x+=dx[i];
v.y+=dy[i]; if(v.dic==-)v.dic=i;
else if(v.dic!=i)
{
v.dic=i;v.chance++;
}
if(v.chance>k)continue; if(inmap(v.x,v.y)&&v.chance<=vis[v.x][v.y]&&mp[v.x][v.y]=='.' )//这里要是改成mp[v.x][v.y]==mp[ex][ey]会错
{
q.push(v);
vis[v.x][v.y]=v.chance;
}
}
}
printf("no\n");
} int main()
{
int cas;cin>>cas;
while(cas--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",mp[i]+); scanf("%d%d%d%d%d",&k,&sy,&sx,&ey,&ex);
bfs();
}
}
逃离迷宫 HDU1728 (bfs)的更多相关文章
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- hdu1072 逃离迷宫系列 bfs
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1072/ 题意:逃离迷宫,路中可能有炸弹,总时间是6个单位,在有炸弹的位置,如果到达的时刻时间大于0,则恢复到6时 ...
- HDU 1728 逃离迷宫(DFS||BFS)
逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...
- HDU 1728 逃离迷宫(BFS)
Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...
- 逃离迷宫(BFS)题解
Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...
- 2018年长沙理工大学第十三届程序设计竞赛 G 逃离迷宫 【BFS】
链接:https://www.nowcoder.com/acm/contest/96/G 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- hdu1242 又又又是逃离迷宫(bfs模板题)
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...
- [HIHO1328]逃离迷宫(bfs,位压)
题目链接:http://hihocoder.com/problemset/problem/1328 这个题bfs到时候不止要存当前的坐标,还要存当前有哪几把钥匙.因为5把钥匙,所以可以直接用位来存,这 ...
- HDU 1728 逃离迷宫【BFS】
题意:给出一个起点,一个终点,规定的转弯次数,问能否在规定的转弯次数内到达终点--- 这一题是学(看)习(题)的(解)@_@ 主要学了两个地方 一个是剪枝,如果搜到的当前点的转弯次数小于该点turn数 ...
随机推荐
- 使用 CSS3 的 box-sizing 属性设置元素大小包含 border 与 padding
Ø 默认情况下,内部元素(如:input)的宽度或高度,是不会包含元素的边框和内边距的,这时就需要使用 box-sizing 属性设置该元素. Ø box-sizing 是 CSS3 的属性,可以 ...
- 使用Sphinx生成本地的Python帮助文档
第一步:安装Sphinx 首先我们需要安装Sphinx,如果已经安装了Anaconda,那么只需要使用如下命令即可安装,关于其中的参数 -c anaconda,可以在链接[1]中查看: conda i ...
- 位运算&,逻辑与and
在python中,0是否 >>> True and True True >>> True and True True >>> 2 and 4 4 ...
- CentOS6.4下Mysql数据库的安装与配置
原文连接:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 说到数据库,我们大多想到的是关系型数据库,比如 ...
- mysql 原理 ~ double write
一 简介:今天来聊聊double write 二 细节 1 Double write 是InnoDB在 tablespace(ibdata1)上的128个页(2个区)是2MB: 2 何谓页断裂 所谓页 ...
- tidb 架构 ~Tidb学习系列(1)
一 简介:今天来研究Tidb 二 安装测试: 0 下载Tidb wget http://download.pingcap.org/tidb-latest-linux-amd64.tar.gz 按如 ...
- css scrollbar样式设置
参考链接:https://segmentfault.com/a/1190000012800450
- 给bootstrap table设置行列单元格样式
1.根据单元格或者行内其他单元格的内容,给该单元格设置一定的css样式 columns: [{ field: 'index', title: '序号', align:"center" ...
- SpringBoot2.x使用Dev-tool热部署
SpringBoot2.x使用Dev-tool热部署 为什么使用热部署? 当修改某些文件内容如配置文件时,我们需要重新启动服务器,比较麻烦,需要一个工具来进行检测是否修改.热加载可以检测到修改的部分, ...
- numpy中 array数组的shape属性
numpy.array 的shape属性理解 在码最邻近算法(K-Nearest Neighbor)的过程中,发现示例使用了numpy的array数组管理,其中关于array数组的shape(状态)属 ...