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 ...
随机推荐
- C#中使用throw和throw ex抛出异常的区别
通常,我们使用try/catch/finally语句块来捕获异常,就像在这里说的.在抛出异常的时候,使用throw和throw ex有什么区别呢? 假设,按如下的方式调用几个方法: →在Main方法中 ...
- 禁止浏览器backspace键(退格键)时跳转页面(extjs,javascript)
Ext实现方式: //方法一 var key = new Ext.KeyMap(document,{ key: 8, fn: function(obj,e){ var type = e ...
- T4:使用 T4 消除程序和配置文件中重复的字符串信息
背景 我们经常在配置文件中配置各种:id.name,然后在程序中使用这些配置获取信息,这导致了字符串重复出现在系统的多个地方,非常不利于维护,本文介绍采用 T4 来消除这种重复. T4 消除重复 配置 ...
- SharePoint 获取详细Log信息
在SharePoint的运维当中,我们可能经常会遇到排错,但是即使找到日志,也不是特别的详细,我们还是需要各种无厘头的猜测. 其实,SharePoint是可以打开详细的日志的,尤其是面对一些服务产生的 ...
- 绝望的主妇第八季/Desperate Housewives迅雷下载
绝望的主妇 第七季 Desperate Housewives Season 8(2011) 本季看点:曾经在<主妇>中有过重要演出的达娜·德拉尼(Dana Delany), 凯尔·麦克拉克 ...
- fabric-ca-client
fabric-ca-client enroll -u http://admin:adminpw@localhost:7054 /root/.fabric-ca-client:总用量 12-rwxr-x ...
- 用IO流向存储器或SD卡中存入/读取字符的工具类
FileManager package com.kale.utils; import java.io.BufferedReader; import java.io.File; import java. ...
- 《Redis设计与实现》
<Redis设计与实现> 基本信息 作者: 黄健宏 丛书名: 数据库技术丛书 出版社:机械工业出版社 ISBN:9787111464747 上架时间:2014-6-3 出版日期:2014 ...
- [Web 前端 ] ES6 == ES 2015
cp from : https://www.cnblogs.com/ricoliu/p/5996149.html 遇到了一个困惑 原来称作es6的现在突然变成es2015 了 原因是这个事ecma ...
- HTTPS安全证书介绍
IIS配置web SSL 安全证书Https访问 From : http://cao416451347ming.blog.163.com/blog/static/1154556162010217441 ...