逃离迷宫(BFS)题解
第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对应行。
思路:
最难的是转弯的思考方法,本来想的是给设置一个结构体内的变量turn以1 2 3 4来设置方向,如果turn与上一个节点不同就说明转向。但是后来看别人的题解说是如果走,就一条路径走到底,并标记,如果继续走走到没有标记的点上就说明转弯了
还有,队列要初始化!!!因为这个一直WA,心态爆炸...
#include<stdio.h>
#include<string.h>
//#include<math.h>
#include<stdlib.h>
#include<queue>
#define N 105
using namespace std;
char map[N][N];
bool vis[N][N];
int ans,m,n,x1,x2,y1,y2,k,to[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
struct node{
int x,y,t;
};
queue<node> q;
int bfs(){
int i;
node a,next,o;
a.x=x1,a.y=y1,a.t=-1;
q.push(a);
vis[x1][y1]=1;
while(!q.empty()){
a=q.front();
q.pop();
if(a.x==x2 && a.y==y2 && a.t<=k) return 1;
for(i=0;i<4;i++){
next.x=a.x+to[i][0];
next.y=a.y+to[i][1];
while(next.x>=1 && next.x<=n && next.y<=m && next.y>=1 && map[next.x][next.y]!='*'){
if(vis[next.x][next.y]==0){
next.t=a.t+1;
vis[next.x][next.y]=1;
q.push(next);
}
o.x=next.x+to[i][0];
o.y=next.y+to[i][1];
next=o;
}
}
}
return 0;
}
int main(){
int w,i,j;
scanf("%d",&w);
while(w--){
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++) scanf("%s",map[i]+1);
scanf("%d%d%d%d%d",&k,&y1,&x1,&y2,&x2);
while(!q.empty()) q.pop(); //注意要初始化
memset(vis,0,sizeof(vis));
ans=bfs();
if(ans) printf("yes\n");
else printf("no\n");
}
return 0;
}
逃离迷宫(BFS)题解的更多相关文章
- hdu 1728 逃离迷宫 (BFS)
逃离迷宫 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- hdu 1728 逃离迷宫 bfs记转向
题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Mem ...
- hdu 1728 逃离迷宫 bfs记步数
题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Mem ...
- hdu_1728_逃离迷宫(bfs)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意:走迷宫,找最小的拐角 题解:对BFS有了新的理解,DFS+剪枝应该也能过,用BFS就要以拐 ...
- hdu1728 逃离迷宫bfs
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1728/ 关于广度优先搜索的第一篇题解.广度优先搜索,就是状态树的层次遍历,一层一层的搜索,直到搜索到目标状态为止 ...
- 杭电 逃离迷宫 BFS
给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位 ...
- HDU 1728 逃离迷宫 BFS题
题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...
- hdu 1728 逃离迷宫 BFS加优先队列 DFS()
http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...
- 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 ...
随机推荐
- 2018/03/09 每日一学PHP 之 require_once require include include_once 包含文件的区别
require_once require include include_once 方法的区别 对于包含文件来说,如果只是使用框架来说的话,应该会很少碰到,因为框架底层对于文件的引用等做了很好的封装, ...
- 自定义vueawesomeswiper分页器样式
swiperOption: {//swiper的配置项 notNextTick: true,//想获得swiper实例对象,这个必须为true direction: 'vertical', // gr ...
- 如何下载Bilibili视频
方法1: https://www.bilibili.com/video/av25940642 (源网址) https://www.ibilibili.com/video/av25940642 (新网址 ...
- php 中date显示时间不对与Linux文件乱码问题
php 中date显示时间不对解决办法如下1.修改/etc/php.ini文件 在里头中找到data.timezone =去掉它前面的分号';' 然后设置data.timezone = “Asia/S ...
- 异常处理:No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer
No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no pro ...
- struts2 OGNL(Object-Graph Navigation Language) 井号,星号,百分号
1.“#”主要有三种用途: 访问OGNL上下文和Action上下文,#相当于ActionContext.getContext():可以访问这几个ActionContext中的属性. parameter ...
- [py]字符串/列表
去除str首尾空格(切片) ## str长度 循环,判断 ### [:i] [i:] 记录位置点 ## 方法1 def trim2(s): s2 = "" start = 0 en ...
- 图结构练习——判断给定图是否存在合法拓扑序列(sdutoj)
#include<stdio.h>#include<string.h>int d[15],map[15][15],vis[15];int main(){ int i,j, ...
- HTML <input> <button> <submit>
定义和用法 value 属性规定与按钮关联的初始值.请始终为按钮规定 type 属性,如果不填默认值是 "submit".submit其实就是一个特殊的button. <bu ...
- Summary: Depth-first Search(DFS)
There are generally two methods to write DFS algorithm, one is using recursion, another one is using ...