hdu 1728 逃离迷宫 bfs记转向
题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728
逃离迷宫
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18702 Accepted Submission(s): 4526
gloria能从一个位置走到另外一个位置吗?
第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行。每行包含n个字符,当中字符'.'表示该位置为空地,字符'*'表示该位置为障碍。输入数据中仅仅有这两种字符,每组測试数据的最后一行为5个整数k, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤
m),当中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,当中x1。x2相应列,y1, y2相应行。
2
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3
no
yes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define INF 999999999
#define eps 0.00001
#define LL __int64
#define pi acos(-1.0) struct point
{
int x,y;
int step;//记录转弯数。
};
int vis[110][110];
int n,m;
int dir[4][2]={
1,0,
-1,0,
0,1,
0,-1
}; char mp[110][110];
int ok(point nw)
{
if(nw.x>=0&&nw.x<n&&nw.y>=0&&nw.y<m&&mp[nw.x][nw.y]=='.')
return 1;
return 0;
}
int sx,sy,ex,ey; int bfs()
{
memset(vis,0,sizeof vis);
point sta,nw,nex;
sta.x=sx;
sta.y=sy;
sta.step=-1;//第一次不算转弯
queue<point>q;
q.push(sta);
while(!q.empty())
{
nw=q.front();
if(nw.x==ex&&nw.y==ey)
return max(0,nw.step);
q.pop();
for(int i=0;i<4;i++)
{
nex=nw;
nex.x+=dir[i][0];
nex.y+=dir[i][1];
nex.step=nw.step+1;//由于是走到了尽头了。所以每一次
//每次step仅仅加1,所以能够用bool vis
while(ok(nex))
{
if(vis[nex.x][nex.y]==0)
{
q.push(nex);
vis[nex.x][nex.y]=1;//停在这个点。
}
nex.x+=dir[i][0];
nex.y+=dir[i][1];
}
}
}
return 999999;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",mp[i]);
int k;
scanf("%d%d%d%d%d",&k,&sy,&sx,&ey,&ex);
sy--,sx--,ey--,ex--;
int ans=bfs();
if(k>=ans)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
/*
2
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3
*/
hdu 1728 逃离迷宫 bfs记转向的更多相关文章
- hdu 1728 逃离迷宫 bfs记步数
题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Mem ...
- hdu 1728 逃离迷宫 (BFS)
逃离迷宫 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- hdu 1728 逃离迷宫 BFS加优先队列 DFS()
http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...
- HDU 1728 逃离迷宫 BFS题
题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...
- HDU 1728 逃离迷宫(DFS||BFS)
逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...
- HDU 1728 逃离迷宫(DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) ...
- HDU 1728 逃离迷宫
[题目描述 - Problem Description] 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,glo ...
- HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 1728:逃离迷宫(DFS,剪枝)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- ProFTPd Local pr_ctrls_connect Vulnerability - ftpdctl 漏洞及攻击代码分析
攻击代码网址:http://www.exploit-db.com/exploits/394/ 1.执行环境: 1.ProFTPD 1.3.0/1.3.0a 2.编译ProFTPD时.--enable- ...
- WinForm多语言版本实战项目演练
一.课程介绍 关于如何实现“WinForm多语言版本”网上有很多实现技术方案,可以说是“琳琅满目”,"包罗万象".俗话说的好:一千个读者就有一千个哈姆雷特!如果您工作中恰好也遇到这 ...
- eclipse配置问题汇总
问题1:fatjar安装出现故障 问题描写叙述:由于要打包包括第三方jar包的工程,需下载eclipse插件.一般下载地址:http://sourceforge.net/projects/fjep/ ...
- iOS App与iTunes文件传输的方法和对iOS App文件结构的说明
转:http://www.xiaoyaoli.com/?p=368 就像很多iOS上面的播放器App一样,本文编写一个程序可以通过iTunes往里面放文件,比如编写一个音乐播放器程序,通过itune往 ...
- Hybrid App 开发初探:使用 WebView 装载页面
Hybrid App 是混合模式应用的简称,兼具 Native App 和 Web App 两种模式应用的优势,开发成本低,拥有 Web 技术跨平台特性.目前大家所知道的基于中间件的移动开发框架都是采 ...
- 一文犀利看懂中美贸易战 z
如今的中国面对着前所未有的经济全球化的大环境.面对着如何成为创新性国家的重任. 钛媒体注:美国东部时间 7 月 6 日凌晨0:01 分,美国正式开始对 340 亿美元的中国产品加征 25% 的关税,这 ...
- Unity5.x shader打包AssetBundle总结
最近比较忙,好久没有更新博客了,新项目切换到unity5.x后使用了新的打包机制,在打包shader的时候遇到了一些问题,这里来记录一下吧. 在上一个项目中,我们使用unity4.7,对于shader ...
- IT狂人第一至四季/全集The IT Crowd迅雷下载
本季第一至四季 The IT Crowd (2006)看点:<IT狂人>史上最囧,最雷,最脑残,最出乎意料,最不按常理出牌的IT “精英们”登上银屏了.让超擅长收发邮件.单击和双击鼠标的I ...
- Lua date转秒数
之前写过一篇关于把秒转换成指定的日期格式 Lua date format 接到一个需求,需要从配置文件中读取活动显示时间段:startDate ~ endDate(格式为:yyyy-mm-dd H ...
- Apache Ant 简介和配置
Apache Ant 简介 Apache Ant是目前事实上的Java应用的标准build脚本工具.使它大受欢迎的一个主要愿意是它的和灵活,允许程序员创建自己的Task来对Ant进行扩展. ...