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 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...
随机推荐
- jenkins+svn+pipeline+kubernetes部署java应用(二)
在jenkins中只能通过http的方式获取svn的数据,所以需要配置svn的http访问方式 一.安装http服务端和mod_dav_svn插件 由于Subversion需要版本化的控制,因此标准的 ...
- day3-python 登录
import datetime # 1. f = open('users') result = f.read() f.close() user_list = result.split() # user ...
- 怎样处理jmeter中文乱码
jmeter返回 中文乱码: 1.在jmeter的bin目录下,找到jmeter的配置文件,jmeter.properties,然后把 sampleresult.default.encoding=UT ...
- linux 上安装配置l2tp的客户端
有些时候我们外网linux服务器需要访问内网的服务器,这时候就需要在外网服务器上配置l2tp的客户端,连接到VPN访问内网服务器. 安装: yum -y install xl2tpd ppp 安装成功 ...
- GoF23种设计模式之行为型模式之责任链模式
一.概述 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并且沿着这条链传递请求,直到有一个对象处理它为止.其设计思想是:给对多个对象处理一个请求的机会, ...
- json.dumps ensure_ascii 方法
在使用json.dumps时要注意一个问题 import json print (json.dumps('中国')) "\u4e2d\u56fd" 输出的会是 '中国' 中 ...
- (转)rvm安装与常用命令
rvm是一个命令行工具,可以提供一个便捷的多版本ruby环境的管理和切换. https://rvm.io/ 如果你打算学习ruby/rails, rvm是必不可少的工具之一. 这里所有的命令都是再用户 ...
- solr7.7.1完整教程
安装 上传solr-7.7.1.tgz至服务器 opt文件加下 解压 tar -zxvf solr-7.7.1.tgz 运行 进入到加压后的文件夹/opt/solr-7.7.1,执行一下命令启动sol ...
- Gym - 101775L SOS 博弈 找规律
题目:https://cn.vjudge.net/problem/Gym-101775L PS:训练赛中被这道题折磨的不轻,和队友反复推必胜态与必败态试图推导出公式或者规律,然后推的心态逐渐失控,,, ...
- Kattis - doubleclique (图论)
From : North American Invitational Programming Contest 2018 给你一个图,以及它的补图.如果部分点在原图中是团,并且其他的所有点在补图中也是团 ...