hdu1728 逃离迷宫
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 逃离迷宫的更多相关文章
- Hdu1728 逃离迷宫 2017-01-17 10:56 81人阅读 评论(0) 收藏
逃离迷宫 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- DFS(5)——hdu1728逃离迷宫
一.题目回顾 题目链接:逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地 ...
- hdu1728逃离迷宫 (利用最短路径思想+优先队列(BFS))
Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...
- hdu1728 逃离迷宫---转弯次数不超过k+BFS
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目大意: 给你一幅图,给出起点终点和最大转弯次数,判断是否能从起点到终点.'*'表示障碍物. ...
- hdu1728 逃离迷宫bfs
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1728/ 关于广度优先搜索的第一篇题解.广度优先搜索,就是状态树的层次遍历,一层一层的搜索,直到搜索到目标状态为止 ...
- hdu 1728:逃离迷宫(DFS,剪枝)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- hdoj 1728 逃离迷宫
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 逃离迷宫(HDU 1728 BFS)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- 关于LED效率,这4点你应该知道
关于LED效率,这4点你应该知道 发布时间:2017-08-22 12:09:35 原创:中国LED网 内容概要: 1. 这些灯的一些光通过转换器或磷光体转换成较长波长(绿色.黄色和红色光)的光,将所 ...
- 解析 STM32 的库函数
解析 STM32 的库函数意法半导体在推出 STM32 微控制器之初,也同时提供了一套完整细致的固件开发包,里面包含了在 STM32 开发过程中所涉及到的所有底层操作.通过在程序开发中引入这样的固件开 ...
- 讲一下Asp.net core MVC2.1 里面的 ApiControllerAttribute (转载)
ASP.NET Core MVC 2.1 特意为构建 HTTP API 提供了一些小特性,今天主角就是 ApiControllerAttribute. (注:文章是18年2月份的,所以文章提到了cor ...
- Zookeeper-集群与单机实践
我用的是linux,CentOS7.3,zookeeper的版本是3.4.6,工具XShell.上传zookeeper的压缩包后我们开始操作. 集群模式: 1.解压zookeeper,路径随意 tar ...
- Nginx 反向代理 上传大文件报 413
Nginx 中上传文件限制是 2m,上传太大就报错,配置一下 client_max_body_size 1024m; 就可以上传 1G 大小文件 添加在 location 中,如果是反向代理就添加在反 ...
- Xamarin开发的一个简单画图程序分享
最近Xamarin比较火,于是稍微看了下,感觉接触过MVC的都应该能很快上手,还挺有意思,于是忍不住写了个简单的画图程序,之前看帖子有人说装不上或者无法部署,估计我比较幸运,编译完了一次就安装成功了, ...
- CSS 分类 (Classification) 实例
CSS 分类 (Classification) 实例CSS 分类属性 (Classification)CSS 分类属性允许你控制如何显示元素,设置图像显示于另一元素中的何处,相对于其正常位置来定位元素 ...
- systemctl添加开机启动
我们对service和chkconfig两个命令都不陌生,systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体. systemctl is-enable ...
- vue项目环境搭建
安装node.js $ npm install -g vue-cli $ vue init webpack my-project ?Project name ?Project description ...
- Daily Scrumming* 2015.12.22(Day 14)
一.团队scrum meeting照片 二.成员工作总结 姓名 任务ID 迁入记录 江昊 任务1112 无 任务说明 今天没有写前端界面,而是完成了跨域请求的实现以及用户实名认证API 前后端大部分数 ...