和连连看非常相似   都是求转向的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)的更多相关文章

  1. HDU 1728:逃离迷宫(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description   给定一个m × n (m行, n列)的迷宫,迷宫中有 ...

  2. hdu1072 逃离迷宫系列 bfs

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1072/ 题意:逃离迷宫,路中可能有炸弹,总时间是6个单位,在有炸弹的位置,如果到达的时刻时间大于0,则恢复到6时 ...

  3. HDU 1728 逃离迷宫(DFS||BFS)

    逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...

  4. HDU 1728 逃离迷宫(BFS)

    Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...

  5. 逃离迷宫(BFS)题解

    Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...

  6. 2018年长沙理工大学第十三届程序设计竞赛 G 逃离迷宫 【BFS】

    链接:https://www.nowcoder.com/acm/contest/96/G 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  7. hdu1242 又又又是逃离迷宫(bfs模板题)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...

  8. [HIHO1328]逃离迷宫(bfs,位压)

    题目链接:http://hihocoder.com/problemset/problem/1328 这个题bfs到时候不止要存当前的坐标,还要存当前有哪几把钥匙.因为5把钥匙,所以可以直接用位来存,这 ...

  9. HDU 1728 逃离迷宫【BFS】

    题意:给出一个起点,一个终点,规定的转弯次数,问能否在规定的转弯次数内到达终点--- 这一题是学(看)习(题)的(解)@_@ 主要学了两个地方 一个是剪枝,如果搜到的当前点的转弯次数小于该点turn数 ...

随机推荐

  1. 数据库的一致性读,赃读,多线程与赃读,ACID,UNDO

    赃读 对于对象额同步异步方法,我们在设计自己的程序的时候,一定要考虑的问题整体,不然会出现数据不一致的错误,很经典的就是赃读(dityread) 示例: ​ package com.nbkj.thre ...

  2. 第14月第17天 automaticallyAdjustsScrollViewInsets contentInsetAdjustmentBehavior

    1. automaticallyAdjustsScrollViewInsets self.edgesForExtendedLayout = UIRectEdgeNone; if ([self resp ...

  3. [转]gcc -ffunction-sections -fdata-sections -Wl,–gc-sections 参数详解

    背景 有时我们的程序会定义一些暂时使用不上的功能和函数,虽然我们不使用这些功能和函数,但它们往往会浪费我们的ROM和RAM的空间.这在使用静态库时,体现的更为严重.有时,我们只使用了静态库仅有的几个功 ...

  4. linux 下camera调试笔记【转】

    转自:https://blog.csdn.net/kevinx_xu/article/details/8801931 linux camera调试 2011-10-23 10:43:37|  分类:  ...

  5. phantomjs 中如何使用xpath

    function getNodeInfo(inputcsvPath) { var htmlnodeInfo = page.evaluate(function () { //_Ltg var XPATH ...

  6. Redis消息通知(任务队列和发布订阅模式)

    Redis学习笔记(十)消息通知(任务队列和发布订阅模式) 1. 任务队列 1.1 任务队列的特点 任务队列:顾名思义,就是“传递消息的队列”.与任务队列进行交互的实体有两类,一类是生产者(produ ...

  7. 读SRE Google运维解密有感(一)

    前言 这几天打算利用碎片时间读了一下"SRE Google运维解密"这本书,目前读了前几章,感觉收获颇多,结合自己的工作经历和书中的要点,写一些感悟和思考 SRE 有关SRE我就不 ...

  8. linux下ssh远程连接工具SecureCRT和xshell编码设置

    默认的编码有时候显示乱码,需要切换到utf-8 xshell的设置 多个会话窗口执行同样命令 中文界面:

  9. 开源监控系统整合Nagios+Cacti+Nconf详解

    1.配置好yum源,为了提高效率建议搭建本地yum源 yum install -y http*  yum install -y mysql*  yum install -y php-*  yum in ...

  10. python下的socket常用方法举例

    python下的socket 1.简单的server和client端的socket代码 server.py: #!/usr/bin/env python #_*_ coding:utf-8 _*_ i ...