给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?

Input  第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中, 
  第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x 1, y 1, x 2, y 2 (1 ≤ k ≤ 10, 1 ≤ x1, x 2 ≤ n, 1 ≤ y 1, y 2 ≤ m),其中k表示gloria最多能转的弯数,(x 1, y 1), (x 2, y2)表示两个位置,其中x 1,x 2对应列,y 1, y 2对应行。 
Output  每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。Sample Input

2
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3

Sample Output

no
yes
解题思路:
bfs,标记下转弯
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std; #define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=;while(q){if(q&)ans=ans*p;p=p*p;q>>=;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
#define mem(x,v) memset(x,v,sizeof(x))
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=,fh=;while((rx<''||rx>'')&&rx!='-')rx=getchar();if(rx=='-')fh=-,rx=getchar();while(rx>=''&&rx<='')ra*=,ra+=rx-,rx=getchar();return ra*fh;}
const int Max = ;
char map[Max][Max];
int vis[Max][Max];
int m,n,s_x,s_y,e_x,e_y,k,flag;
int dir[][]={{,},{,},{-,},{,-}};
struct node{
int x,y;
int num;
int dir;
}s_pos;
int check(int x,int y){
if(x>=&&y>=&&x<m&&y<n&&map[x][y]!='*')
return ;
else
return ;
}
void dfs(){
s_pos.x = s_x;s_pos.y=s_y;s_pos.dir=-;s_pos.num=-;
vis[s_x][s_y] = ;
queue<node>q;
q.push(s_pos);
while(!q.empty()){
node now = q.front();q.pop();
if(now.x==e_x&&now.y==e_y&&now.num<=k){
flag=; return ;
}
for(int i=;i<;i++){
node next = now;
next.x += dir[i][]; next.y += dir[i][]; if(check(next.x,next.y)){ if(next.dir == -){
next.dir = i;
next.num = ;
if(vis[next.x][next.y]>=next.num){
vis[next.x][next.y] = next.num;
q.push(next);
}
}
else{
if(i==now.dir){
if(vis[next.x][next.y]>=next.num){
vis[next.x][next.y] = next.num;
q.push(next);
}
} else{
next.num+=; next.dir=i; //此时转弯num+1
if(vis[next.x][next.y]>=next.num&&next.num<=k){
vis[next.x][next.y] = next.num;
q.push(next);
}
}
}
}
}
}
} int main()
{
int t;
cin>>t;
while(t--){
cin>>m>>n;
for(int i=;i<m;i++)
cin>>map[i];
for(int i=;i<m;i++)
for(int j=;j<n;j++)
vis[i][j] = ;
cin>>k>>s_y>>s_x>>e_y>>e_x;
s_y-=;s_x-=;e_y-=;e_x-=;
flag = ;
dfs();
if(s_x==e_x&&s_y==e_y){
cout<<"yes"<<endl;continue;
}
if(flag)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
}

hdu1728 逃离迷宫的更多相关文章

  1. Hdu1728 逃离迷宫 2017-01-17 10:56 81人阅读 评论(0) 收藏

    逃离迷宫 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  2. DFS(5)——hdu1728逃离迷宫

    一.题目回顾 题目链接:逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地 ...

  3. hdu1728逃离迷宫 (利用最短路径思想+优先队列(BFS))

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

  4. hdu1728 逃离迷宫---转弯次数不超过k+BFS

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目大意: 给你一幅图,给出起点终点和最大转弯次数,判断是否能从起点到终点.'*'表示障碍物. ...

  5. hdu1728 逃离迷宫bfs

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1728/ 关于广度优先搜索的第一篇题解.广度优先搜索,就是状态树的层次遍历,一层一层的搜索,直到搜索到目标状态为止 ...

  6. hdu 1728:逃离迷宫(DFS,剪枝)

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

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

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

  8. hdoj 1728 逃离迷宫

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

  9. 逃离迷宫(HDU 1728 BFS)

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

随机推荐

  1. [03-01] JSP自定义标签

    1.自定义标签的概念 目前我们在JSP中使用的标签都是HTML的标签,浏览器会自动解析运行,例如<form action=""></form>,这里的for ...

  2. 利用shell脚本或者php移动某个文件夹下的文件到各自的日期组成的目录下

    背景是这样的:网站一开始访问量比较小,大家就把所有的图片文件上传到一个目录下(比如是/data/images/).后来访问量大了,图片也多了,这样就影响读取效率.所以有个这样的需求,把这些个图片文件移 ...

  3. 如何传递参数给ASP.NET Core的中间件(Middleware)

    问题描述 当我们在ASP.NET Core中定义和使用中间件(Middleware)的时候,有什么好的办法可以给中间件传参数吗? 解决方案 在ASP.NET Core项目中添加一个POCO类来传递参数 ...

  4. Combobox值自定义(不通过数据库)

    前台 <ext:ComboBox ID="cmbYear" runat="server"/> .aspx.cs int year = DateTim ...

  5. python中使用pymongo操作mongo

    MongoDB是由C++语言编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容存储形式类似JSON对象,它的字段值可以包含其他文档.数组及文档数组,非常灵活.在这一节中,我们就来看 ...

  6. 【JVM.11】Java内存模型与线程

    鲁迅曾经说过“并发处理的广泛应用是使得Amdahl定律代替摩尔定律成为计算机性能发展源动力的根本原因,也是人类‘压榨‘ 计算机运行能力的最有力武器.” 一.概述 多任务处理在现代计算机操作系统中几乎已 ...

  7. 【亲测有效】Github无法访问或者访问速度的解决方案

    我相信,很多朋友都遇到了 Github 访问速度过慢的问题,我也是在此记下笔记,方便以后拿来使用. 第一步.修改Hosts 通过问题的搜索了解到 github 访问很慢一般通过修改 hosts 文件解 ...

  8. 快速零配置迁移 API 适配 iOS 对 IPv6 以及 HTTPS 的要求

    本文快速分享一下快速零配置迁移 API 适配 iOS 对 IPv6 以及 HTTPS 的要求的方法,供大家参考. 原文发表于我的技术博客 零配置方案 最新的苹果审核政策对 API 的 IPv6 以及 ...

  9. javaScript常用API合集

    节点 1.1 节点属性 Node.nodeName   //返回节点名称,只读 Node.nodeType   //返回节点类型的常数值,只读 Node.nodeValue  //返回Text或Com ...

  10. Scrum Meeting NO.9

    Scrum Meeting No.9 1.会议内容 2.任务清单 徐越 序号 近期的任务 进行中 已完成 1 代码重构:前端通讯模块改为HttpClient+Json √ 2 "我" ...