传送门

逃离迷宫

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1253 1072 1026 1372 1180 
 
 
题解:
dfs,记得用vis记录,防止重复搜索
13261396 2015-03-27 21:05:25 Accepted 1728 93MS 4272K 2241 B G++ czy
 #include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <map>
#include <algorithm> #define ll long long
int const N = ;
int const M = ;
int const inf = ;
ll const mod = ; using namespace std; int T;
int n,m;
int k;
int vis[N][N][][]; struct PP
{
int x;
int y;
int operator ==(const PP &b) const
{
if(x==b.x && y==b.y) return ;
else return ;
}
}; PP st,en;
char s[N][N];
int dirx[]={-,,,};
int diry[]={,,,-};
int flag; void ini()
{
int i,j;
flag=;
scanf("%d%d",&n,&m);
memset(vis,,sizeof(vis));
for(i=;i<=n+;i++){
for(j=;j<=m+;j++){
s[i][j]='*';
}
}
for(i=;i<=n;i++){
scanf("%s",s[i]+);
}
scanf("%d%d%d%d%d",&k,&st.y,&st.x,&en.y,&en.x);
} void dfs(PP te,int d,int num)
{ if(num>k){
return;
}
if(te==en){
flag=;return;
}
int i;
PP nt;
for(i=;i<;i++){
nt.x=te.x+dirx[i];
nt.y=te.y+diry[i];
if(s[nt.x][nt.y]=='.'){
if(i==d){
if(vis[nt.x][nt.y][num][i]==) continue;
vis[nt.x][nt.y][num][i]=;
dfs(nt,i,num);
}
else{
if(vis[nt.x][nt.y][num+][i]==) continue;
vis[nt.x][nt.y][num+][i]=;
dfs(nt,i,num+);
}
if(flag==) return;
}
}
} void solve()
{
int d;
PP nt;
if(nt==en){
flag=;return;
}
for(d=;d<;d++){
if(flag==) return;
nt.x=st.x+dirx[d];
nt.y=st.y+diry[d];
if(s[nt.x][nt.y]=='.'){
vis[nt.x][nt.y][][d]=;
dfs(nt,d,);
}
}
} void out()
{
if(flag==){
printf("yes\n");
}
else{
printf("no\n");
}
} int main()
{
//freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
while(T--)
//while(scanf("%d%d",&n,&sum)!=EOF)
{
ini();
solve();
out();
}
}

hdu 1728 逃离迷宫 [ dfs ]的更多相关文章

  1. HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)

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

  2. HDU 1728 逃离迷宫(DFS||BFS)

    逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...

  3. hdu 1728 逃离迷宫 bfs记转向

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  4. hdu 1728 逃离迷宫 bfs记步数

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  5. HDU 1728 逃离迷宫(DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)  ...

  6. hdu 1728:逃离迷宫(DFS,剪枝)

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

  7. HDU 1728 逃离迷宫

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

  8. hdu 1728 逃离迷宫 (BFS)

    逃离迷宫 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  9. hdu 1728 逃离迷宫(dFS+优先队列)

    求转弯最少的走路方式!!!! #include<stdio.h> #include<string.h> #include<queue> using namespac ...

随机推荐

  1. EJB2的配置

    1. ejb-jar.xml <?xml version="1.0" encoding="UTF-8"?> <ejb-jar xmlns=&q ...

  2. jQuery ajax参数后台获取不到的问题

    <script type="text/javascript"> init(); var alldate = {a : "0",b:"1&q ...

  3. js由浅入深理解

    隐式转换 + - num - 0 把num转换成number: num + "" 把num转换成字符串: ------------------------------------- ...

  4. LibreOJ #101. 最大流

    题目描述 这是一道模板题. 给定 n nn 个点,m mm 条边,给定每条边的容量,求从点 s ss 到点 t tt 的最大流. 输入格式 第一行四个整数 n nn.m mm.s ss.t tt.接下 ...

  5. SEO 第十章

    SEO第十章 本次课目标: 1.  站外优化方案计划 2.  常见的SEO作弊手段(黑帽) 3.  百度站长平台的使用 4.  网站流量提升和转化率提升 一.站外优化方案计划 友情链接 权重相当.行业 ...

  6. Android(java)学习笔记169:服务(service)之为什么使用服务

    1.服务 service 长期在后台运行的进程,一般没有应用程序界面   2.进程线程和应用程序之间的关系 应用程序开启,系统启动一个Linux进程,所有的组件都是运行在同一个进程的同一个线程(mai ...

  7. 解决selenium.common.exception.WebDriverException:Message:'chromedriver' executable needs to be in Path

    'chromedriver' executable needs to be in Path 声明:本人萌新,刚学python不久记录一下自己的坑,发出来若能帮助到一些人尽早解决问题那便是极好的,( ̄▽ ...

  8. js 数组过滤 filter

    let res = this.list.filter(item => routeEqual(this.currentRouteObj, item) || item.name === this.$ ...

  9. 【整理】用JSON-server模拟REST API

    用JSON-server模拟REST API https://www.cnblogs.com/ys-wuhan/p/6387791.html

  10. app自动化配置信息

    caps={    "platformName":"Android",#平台名称    "platformVersion":"6. ...