逃离迷宫

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22233    Accepted Submission(s): 5413

Problem Description
 
 给定一个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, 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对应行。
 
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
 
Source
 
限制了转弯次数不限制步数的搜索,所以当走到某个点时一直往它原来的方向走到底就行了。注意这个题输入的是 列 行 。。被坑了很多次才AC。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
typedef long long LL;
char graph[][];
int n,m,cnt;
struct Node{
int x,y;
int step;
};
Node s,t;
bool vis[][];
bool check(int x,int y){
if(x<||x>=m||y<||y>=n||graph[x][y]=='*') return false;
return true;
}
int dir[][] = {{,},{-,},{,},{,-}};
bool bfs(){
memset(vis,false,sizeof(vis));
queue<Node> q;
q.push(s);
vis[s.x][s.y] = true;
s.step = ;
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.step>cnt) return false;
if(now.x==t.x&&now.y==t.y) return true;
Node next;
for(int i=;i<;i++){
next.x = now.x+dir[i][];
next.y = now.y+dir[i][];
next.step = now.step+;
while(check(next.x,next.y)){
if(next.x==t.x&&next.y==t.y) return true;
if(vis[next.x][next.y]==false){ ///没访问过进入队列
vis[next.x][next.y]=true;
q.push(next);
}
next.x+=dir[i][]; ///笔直走向下一个点
next.y+=dir[i][];
} }
}
return false;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d",&m,&n);
for(int i=;i<m;i++){
scanf("%s",&graph[i]);
}
scanf("%d%d%d%d%d",&cnt,&s.y,&s.x,&t.y,&t.x); ///莫名的坑
s.x-=,s.y-=,t.x-=,t.y-=;
bool flag = bfs();
if(flag) printf("yes\n");
else printf("no\n");
}
return ;
}

还有一种解法比较好理解,标记每次的方向,vis数组加一维.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N = ;
int n,m;
int x1,y11,x2,y2,k;
char graph[N][N];
struct Node{
int x,y,step,dir;
};
bool vis[N][N][];
int dir[][] = {{-,},{,},{,},{,-}};
bool check(int x,int y,int i){
if(x<||x>n||y<||y>m||vis[x][y][i]||graph[x][y]=='*') return false;
return true;
}
void bfs(){
memset(vis,false,sizeof(vis));
Node s;
s.x = x1,s.y = y11,s.step = ,s.dir = -;
if(graph[s.x][s.y]=='*'||graph[x2][y2]=='*'){
printf("no\n");
return;
}
queue <Node> q;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.step>k){
printf("no\n");
return;
}
if(now.x==x2&&now.y==y2){
printf("yes\n");
return;
}
Node next;
for(int i=;i<;i++){
next.x = now.x + dir[i][];
next.y = now.y + dir[i][];
next.step = now.step;
next.dir = i;
if(now.dir!=-&&now.dir!=next.dir){
next.step = now.step+;
}
while(check(next.x,next.y,i)){
vis[now.x][now.y][i] = true;
vis[next.x][next.y][i] = true;
q.push(next);
next.x = next.x + dir[i][];
next.y = next.y + dir[i][];
next.dir = i;
next.step = next.step;
}
}
}
printf("no\n");
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d",&n,&m);
memset(graph,,sizeof(graph));
for(int i=;i<=n;i++){
scanf("%s",graph[i]+);
}
scanf("%d%d%d%d%d",&k,&y11,&x1,&y2,&x2);
bfs();
}
return ;
}

hdu 1728(搜索)的更多相关文章

  1. hdu 1728 搜索求最少的转向次数

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

  2. hdu 1728

    //hdu 1728 //这个是一道很经典的迷宫题了,思路感觉...取起点和终点,判断连线是否超过n个弯, //先是从起点出发,上下左右四个方向搜索,找到一条路,把那条路的第一个点压入队列 //然后沿 ...

  3. HDU 1728 逃离迷宫

    [题目描述 - Problem Description] 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,glo ...

  4. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. hdu 5636 搜索 BestCoder Round #74 (div.2)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  6. Square HDU 1518 搜索

    Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...

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

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

  8. hdu 1728 逃离迷宫 BFS加优先队列 DFS()

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...

  9. hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...

随机推荐

  1. [BZOJ] 1520: [POI2006]Szk-Schools

    费用流解决. abs内传不了int..CE一次 #include<iostream> #include<cstring> #include<cstdio> #inc ...

  2. sphinx增量索引使用

    sphinx在使用过程中如果表的数据量很大,新增加的内容在sphinx索引没有重建之前都是搜索不到的. 这时可以通过建立sphinx增量索引,通过定时更新增量索引,合并主索引的方式,来实现伪实时更新. ...

  3. windows server2008R2 64位 配置 mysql-8.0.15-winx64

    下载mysql: 1 https://dev.mysql.com/downloads/mysql/ 如图: 然后在解压的文件夹里面新建 my.ini文件,内容如下 按 Ctrl+C 复制代码 按 Ct ...

  4. 【linux】【进程】stand alone 与 super daemon 区别

    本文引用自  鸟哥的linux私房菜如果依据 daemon 的启动与管理方式来区分,基本上,可以将 daemon 分为可独立启动的 stand alone , 与透过一支 super daemon 来 ...

  5. Leetcode 145. 二叉树的后序遍历

    题目链接 https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/ 题目描述 给定一个二叉树,返回它的 ...

  6. UVa 10655 Contemplation! Algebra 矩阵快速幂

    题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ...

  7. python基础学习笔记——网络编程(协议篇)

    一 互联网的本质 咱们先不说互联网是如何通信的(发送数据,文件等),先用一个经典的例子,给大家说明什么是互联网通信. 现在追溯到八九十年代,当时电话刚刚兴起,还没有手机的概念,只是有线电话,那么此时你 ...

  8. js 常用判断

    JS三元运算符 三元运算符: 如名字表示的三元运算符需要三个操作数. 语法是 条件 ? 结果1 : 结果2;. 这里你把条件写在问号(?)的前面后面跟着用冒号(:)分隔的结果1和结果2.满足条件时结果 ...

  9. Nginx快速安装

    登录nginx官网,点击download 点击稳定版本 复制箭头上面的内容 vim /etc/yum.repos.d/nginx.repo 修改yum文件 将复制的内容粘贴上去 yum list | ...

  10. Leetcode23--->Merge K sorted Lists(合并k个排序的单链表)

    题目: 合并k个排序将k个已排序的链表合并为一个排好序的链表,并分析其时间复杂度 . 解题思路: 类似于归并排序的思想,lists中存放的是多个单链表,将lists的头和尾两个链表合并,放在头,头向后 ...