hdu1728逃离迷宫 (利用最短路径思想+优先队列(BFS))
第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x
1, y
1, x
2, y
2 (1 ≤ k ≤ 10, 1 ≤ x
1, x
2 ≤ n, 1 ≤ y
1, y
2 ≤ m),其中k表示gloria最多能转的弯数,(x
1, y
1), (x
2, y
2)表示两个位置,其中x
1,x
2对应列,y
1, y
2对应行。
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3
yes
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
typedef struct nod
{
int direct,turn,x,y;
friend bool operator <(nod n1,nod n2)
{
return n2.turn<n1.turn;
}
}node;
int h,w,sx,sy,dx,dy,maxturn,flog;
int N[105][105];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char map[105][105]; void BFS()
{
int e,tx,ty;
priority_queue<node>Q;
node q,p;
q.x=dx-1;q.y=dy-1;q.direct=-1;q.turn=0;
Q.push(q);N[dy-1][dx-1]=0;
while(!Q.empty())
{
q=Q.top();
Q.pop();
if(q.turn>maxturn)
continue;
for(e=0;e<4;e++)
{
tx=q.x+dir[e][0];ty=q.y+dir[e][1];
if(tx>=0&&tx<w&&ty>=0&&ty<h&&map[ty][tx]!='*')
{
p.direct=e; p.x=tx; p.y=ty; p.turn=q.turn;
if(q.direct>=0&&q.direct!=e)
p.turn++;
if(N[ty][tx]>=p.turn)
{
N[ty][tx]=p.turn;
Q.push(p);//printf("%d %d\n",q.turn,e);
}
}
}
}
if(N[sy-1][sx-1]<=maxturn)
flog=1;
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&h,&w);
getchar();
for(i=0;i<h;i++)
{
for(j=0;j<w;j++)
{
scanf("%c",&map[i][j]);
N[i][j]=10000000;
}
getchar();
}
scanf("%d%d%d%d%d",&maxturn,&sx,&sy,&dx,&dy);
flog=0;
BFS(); printf("%s\n",flog?"yes":"no");
}
}
hdu1728逃离迷宫 (利用最短路径思想+优先队列(BFS))的更多相关文章
- 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
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1728/ 关于广度优先搜索的第一篇题解.广度优先搜索,就是状态树的层次遍历,一层一层的搜索,直到搜索到目标状态为止 ...
- hdu1728 逃离迷宫---转弯次数不超过k+BFS
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目大意: 给你一幅图,给出起点终点和最大转弯次数,判断是否能从起点到终点.'*'表示障碍物. ...
- hdu1728 逃离迷宫
给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位 ...
- HDU1728 从迷宫中逃脱 【方向BFS】
从迷宫中逃脱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- 逃离迷宫(HDU 1728 BFS)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- MYSQL 查看当前用户与数据库版本
问题一: 查看当前用户 select user(); 问题二: 查看数据库版本 select version();
- Myeclipse 2014配置SVN详细图解
1.什么是SVN? 管理软件开发过程中的版本控制工具. 2.myeclipse安装SVN插件步骤,以myeclipse 2014为例. (1)下载SVN插件 http://subclipse.tigr ...
- C#中ref和out的使用小结
ref传递的参数是变量的地址,在传入函数后,函数可以使用这些地址处的值,同时函数执行完后,这些变量被带回了调用者.ref传递的参数既可作传入值,也可作返回值. out传递的参数是变量的地址,在传入函数 ...
- 【POJ 1679 The Unique MST】最小生成树
无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...
- MS SQL 当记录不存在时插入insert INTO not exists
INSERT INTO dbo.[T_DabaoTemp] ([PType] ,[pID] ,[NewVersion] ,[ParentC ...
- C# 泛型转换 将object[] 数组转换为泛型列表
public List<TaskSoftInfo> ReadSoftDownTaskList() { string[] Tid = (strin ...
- [置顶] ANT build.xml文件详解
Ant的优点 跨平台性.Ant是用Java语言编写的,所示具有很好的跨平台性. 操作简单.Ant是由一个内置任务和可选任务组成的. Ant运行时需要一个XML文件(构建文件). Ant通过调用targ ...
- uva 10655 - Contemplation! Algebra
---恢复内容开始--- Given the value of a+b and ab you will have to find the value of an+bn 给出a+b和a*b的值,再给出n ...
- ./configure: error: the HTTP rewrite module requires the PCRE library
docker中CentOS安装nginx出错,提示没有PCRE,需要安装pcre-devel,同时还需要安装openssl.openssl-devel yum -y install pcre-deve ...
- BestCoder Round #75 1003 - King's Order
国王演讲后士气大增,但此时战争还没有结束,国王时不时要下发命令. 由于国王的口吃并没有治愈,所以传令中可能出现:“让第三军-军-军,到前线去” 这样的命令.由于大洋国在军队中安插了间谍 , 战事紧急, ...