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
第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
入队点假设再直走或者后退,那都是会訪问已经訪问的点,是没意义的。所以必会转向,所以转向数是+1的。
#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 ...
随机推荐
- Python之pandas数据加载、存储
Python之pandas数据加载.存储 0. 输入与输出大致可分为三类: 0.1 读取文本文件和其他更好效的磁盘存储格式 2.2 使用数据库中的数据 0.3 利用Web API操作网络资源 1. 读 ...
- Android集成微信分享功能应用签名生成方法及分享不生效的问题
通过友盟sdk集成微博.微信.qq等分享功能时,微博和qq很顺利,但在做微信集成时一直不成功.主要问题还是之前在微信开放平台申请创建移动应用时,对应用签名没有填写对,走了很多弯路现总结出来,加深记忆避 ...
- Errors reported here must be corrected before the service can be started
场景: 安装.配置Apache24时候,最后会给出提示,如图:Errors reported here must be corrected before the service can be star ...
- 关于FLASK WEB开发8d 数据库迁移的问题
首先, 第一步,要删除data-dev.sqlite这个数据库 第二步,进行下面的重建 暂时的解决办法是: python manage.py shell In [2]: from app import ...
- 在iframe内页面完全加载完后,关闭父页面生成的div遮罩层
遮罩层div为iframe父页面生成,需在iframe内页面完全加载完后,关闭遮罩层 alertMsgClose() :函数为关闭遮罩层函数 此段代码在iframe页面内: <script> ...
- 三维卷积:全景图像Spherical CNNs(Code)
卷积神经网络(CNN)可以很好的处理二维平面图像的问题.然而,对球面图像进行处理需求日益增加.例如,对无人机.机器人.自动驾驶汽车.分子回归问题.全球天气和气候模型的全方位视觉处理问题. 将球形信号的 ...
- HiveServer2后台运行
nohup hive --service hiveserver2 & 或者直接: nohup hiveserver2 &
- Java基础(八)--String(源码)、StringBuffer、StringBuilder
String源码:基于jdk1.8 public final class String implements Serializable, Comparable<String>, CharS ...
- 网络编程 - socket接收大数据
通过socket,实现客户端发送命令,将服务端执行出的结果,反回到客户端,主要4个步骤:1.服务端返回数据: 2.服务端返回数据的大小: 3.客户端接收返回数据的大小: 4.客户端按返回数据大小接收数 ...
- C++ 实现Golang里的defer
不多说了,直接贴代码.就一个hpp文件. 1 #include <functional> 2 3 #define CONCAT_(a, b) a##b 4 #define CONCAT(a ...