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 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...
随机推荐
- 【Git版本控制】idea中使用git进行项目管理
转载博文:完整教程-idea使用git进行项目管理(总结版)
- Mysql中反引号和单引号的区别
反引号,一般在ESC键的下方. 它是为了区分MYSQL的保留字与普通字符而引入的符号.举个例子:SELECT `select` FROM `test` WHERE select='字段值'在test表 ...
- MySQL自学笔记_聚集函数
1. 使用场景 很多时候我们需要查找数据库中符合特定条件的数据的计数.最大值.最小值.平均值等一个数字,并需要要导出所有相关数据明细.此时就需要用到聚集函数. 而返回所有数据明细会占用数据库资源和网络 ...
- nginx url rewrite break和last的区别
break 将重写的URI作为一个新的URI,在本块中继续处理,将重写后 的地址在当前location块中处理,不会将新的URI转向到其他location块中 last,终止继续在本location块 ...
- 关于PHP连接池扩展php-cp遇到的那些坑
php-cp是国内大神写的php第三方扩展,具体就不用多说了,细读https://github.com/swoole/php-cp,下面来说说今天安装方法. 环境:CentOS7.2.1511 由于本 ...
- AHB 总线问答(转)
AHB总线问答 http://blog.163.com/huanhuan_hdu/blog/static/1352981182011625916845/ 仲裁:主设备可以在一个突发传输中解除HLOCK ...
- 《零基础入门学习Python》【第一版】视频课后答案第001讲
测试题答案: 0. Python 是什么类型的语言? Python是脚本语言 脚本语言(Scripting language)是电脑编程语言,因此也能让开发者藉以编写出让电脑听命行事的程序.以简单的方 ...
- Python3爬虫一之(urllib库)
urllib库是python3的内置HTTP请求库. ython2中urllib分为 urllib2.urllib两个库来发送请求,但是在python3中只有一个urllib库,方便了许多. urll ...
- 并查集:CDOJ1594-老司机的奇幻漂流 (食物链)
老司机的奇幻漂流 UESTC - 1594 Problem Description 老司机在救出了女票之后,就和她在全世界旅游,有一天,他们来到了一个神奇的小岛上. 这个小岛上有三种动物,他们互相克制 ...
- Linux学习-什么是 daemon 与服务 (service)
『常驻在记体体中的程序,且可以提供 一些系统或网络功能,那就是服务』.而服务一般的英文说法是『 service 』. 那么 daemon 与 service 有关啰?否则为什么都能够提供 某些系统或网 ...