题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728

注意:1、先输入起点(y1,x1)和终点(y2,x2);

2、如果一个一个遍历会超时。

思路:每次将整一行或列的点全部遍历,然后再寻找是否找过某个点。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,x1,y1,x2,y2,k;
char a[][];
struct Node{
int x,y,step;
};
int vis[][],zz[][]={{,},{,-},{,},{-,}};
bool pd(int x,int y)
{
if(x<||x>=n||y<||y>=m||a[x][y]=='*') return false;
return true;
}
void bfs()
{
if(x1==x2&&y1==y2)
{
cout<<"yes"<<endl;
return ;
}
queue <Node> q;
memset(vis,,sizeof(vis));
Node tmp,tp;
tmp.x=x1-;tmp.y=y1-;tmp.step=-;
vis[x1-][y1-]=;
q.push(tmp);
while(!q.empty())
{
tmp=q.front();
q.pop();
for(int i=;i<;i++)
{
tp.x=tmp.x+zz[i][];
tp.y=tmp.y+zz[i][];
while(pd(tp.x,tp.y))
{
if(vis[tp.x][tp.y]==)
{
tp.step=tmp.step+;
vis[tp.x][tp.y]=;
if(tp.x==x2-&&tp.y==y2-&&tp.step<=k)
{
cout<<"yes"<<endl;
return ;
}
q.push(tp);
}
tp.x+=zz[i][];
tp.y+=zz[i][];
}
}
}
cout<<"no"<<endl;
return ;
} int main(void)
{
int i,j,T;
cin>>T;
while(T--)
{
cin>>n>>m;
for(i=;i<n;i++) scanf("%s",a[i]);
cin>>k>>y1>>x1>>y2>>x2;
bfs();
}
return ;
}

参考文章:https://blog.csdn.net/flynn_curry/article/details/52901207

hdu-1728(bfs+优化)的更多相关文章

  1. hdu 1728 bfs **

    简单bfs,记录好状态即可 #include<cstdio> #include<iostream> #include<algorithm> #include< ...

  2. 逃离迷宫(HDU 1728 BFS)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. HDU 1728 逃离迷宫

    [题目描述 - Problem Description] 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,glo ...

  4. hdu 1728

    //hdu 1728 //这个是一道很经典的迷宫题了,思路感觉...取起点和终点,判断连线是否超过n个弯, //先是从起点出发,上下左右四个方向搜索,找到一条路,把那条路的第一个点压入队列 //然后沿 ...

  5. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  6. 2013年第四届蓝桥杯国赛 九宫重排(HashMap+双BFS优化)

    九宫重排     时间限制:1.0s   内存限制:256.0MB 问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干 ...

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

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

  8. hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...

  9. hdu 1728 逃离迷宫 bfs记转向

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  10. HDU - 1728 逃离迷宫 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1728 思路 BFS 一开始 从开始位置 往四周走 如果能走的话 这个时候 转弯次数都是0 我们的标记不 ...

随机推荐

  1. tomcat 服务器发布网站

    第一种方法:在tomcat中的conf目录中,在server.xml中的,<host/>节点中添加: D:\Program Files\Apache Software Foundation ...

  2. jquery 隐藏 显示 动画效果

    <!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11.1.min.js&qu ...

  3. a 超链接标签

    Title百度 第一章 第一章 第一章内容 第二章内容 <!DOCTYPE html><html lang="en"><head> <me ...

  4. 10 python os&sys 模块

    1.os模块 os模块提供了很多允许你的程序与操作系统直接交互的功能 os模块的主要功能:处理文件和目录,系统相关,执行命令,管理进程 检验给出的路径是否是一个文件:os.path.isfile() ...

  5. 低版本eclipse离线集成svn步骤,亲测有效!!!

    1.下载svn离线版的插件: 百度云盘链接:http://pan.baidu.com/s/1eSnMoHO 密码:6oef 2.解压出来的额目录如下: 3.将features和plugins里面的ja ...

  6. Haskell语言学习笔记(24)MonadWriter, Writer, WriterT

    MonadWriter 类型类 class (Monoid w, Monad m) => MonadWriter w m | m -> w where writer :: (a,w) -& ...

  7. 在spring MVC 中关于session失效的判断 有一个类SessionStatus

    SessionStatus status 表示的是当前Session的状态  status.isComplete()-->为true时,表示当前Session还未过期;-->false,表 ...

  8. Spring WebMVC 4.1返回json时 406(Not Acceptable)

    1.问题现象Tomcat7+Spring4.1.4,返回json字符串时发生406错误 The resource identified by this request is only capable ...

  9. SharePoint 2010 外部数据类型

    需求描述: 在sharepoint 2010有时需要访问外部的数据(如sql数据库). 参考文章: 外部内容类型 Business Connectivity Services ECT外部内容类型 Se ...

  10. spring JPA分页排序条件查询

    @RequestMapping("/listByPage") public Page<Production> listByPage(int page, int size ...