hdu 1728(搜索)
逃离迷宫
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22233 Accepted Submission(s): 5413
给定一个m × n (m行,
n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,
她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什
么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以
选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?
第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对应行。
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3
yes
#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(搜索)的更多相关文章
- hdu 1728 搜索求最少的转向次数
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- hdu 1728
//hdu 1728 //这个是一道很经典的迷宫题了,思路感觉...取起点和终点,判断连线是否超过n个弯, //先是从起点出发,上下左右四个方向搜索,找到一条路,把那条路的第一个点压入队列 //然后沿 ...
- HDU 1728 逃离迷宫
[题目描述 - Problem Description] 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,glo ...
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 5636 搜索 BestCoder Round #74 (div.2)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Square HDU 1518 搜索
Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- hdu 1728 逃离迷宫 BFS加优先队列 DFS()
http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...
- hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...
随机推荐
- 【最大流】bzoj1711: [Usaco2007 Open]Dining吃饭
正在网络流入门(原来这种题用网络流做) Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想 ...
- 关于bc中小数点length,scale,(())以及进制转换
这是我在codewar上遇到的一个题,我用我自己的方法做出了解答,如下: 1 #!/bin/bash 2 3 distance=`echo "$1*10000"|bc|cut -d ...
- JQuery图片轮播实例
HTML+CSS代码: <!doctype html> <html> <head> <meta charset="utf-8"> & ...
- DELL PowerEdge R620安装Windows server(你想将windows安装在何处”找不到任何本地磁盘,“找不到驱动器”)已解决!
你可能碰到过DELL服务器上安装Windows server系列系统时无法识别或找不到硬盘的问题,对于DELL PowerEdge11-14代机器的,大家可以采用DELL的Lifecycle cont ...
- COMP9021——6.3
有关yield的用法简介以及图灵机 第一节课大体没有太大变化,前半节课为了给图灵机的讲解做铺垫引入了yield.数组.字符串和文件等都是一个可迭代的对象,但由于它们的所有数据都存储与内存中,对内存的消 ...
- PyCharm2019 激活方式
1.修改hosts激活:需要修改hosts,稳定无影响,持续更新,推荐~ 一.修改hosts激活 1.修改hosts文件 将0.0.0.0 account.jetbrains.com和0.0.0.0 ...
- CRC点滴
研究了一个晚上,大致看懂了crc校验的方法.这里记录一下,因为can总线中需要用到crc校验的. 举例说明CRC校验码的求法:(此例子摘自百度百科:CRC校验码) 信息字段代码为: 1011001:对 ...
- 迷宫问题&MakeFile
先看一个有意思的问题, 我们定义一个二维数组表示迷宫. 它表示一个迷宫, 其中的1表示墙壁,0表示可以走的路, 只能横着走或竖着走,不能斜着走, 我们要编程序找出从左上角到右下角的路线.其实这个问题可 ...
- 5中IO模型整理总结
1.5中IO模型: 阻塞I/O(blocking IO) 非阻塞I/O(noblocking IO) I/O复用 (IO multiplexing ) 信号驱动I/O (signal drive ...
- Python3异常-AttributeError: module 'sys' has no attribute 'setdefaultencoding'
基于python3.6.1版本,在一个.py文件中,加入这3行: import requests, re, sys reload(sys) sys.setdefaultencoding("u ...