传送门

逃离迷宫

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. 在WIN7下解决coursera视频无法播放问题

    https://blog.csdn.net/u012509485/article/details/78459584在WIN7下解决coursera视频无法播放问题2019/1/20 23:18 最近C ...

  2. Failure to transfer org.apache.maven.plugins:maven-compiler-plugin:jar:2.5.1

    Mac上写了一段基于Maven的java代码. 上传Git后,在windows上pull下来,eclipse里面各种错误. ArtifactTransferException:Failure to t ...

  3. 洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day

    题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self ...

  4. COGS 1406. 邻居年龄排序[Age Sort,UVa 11462](水题日常)

    ★   输入文件:AgeSort.in   输出文件:AgeSort.out   简单对比时间限制:1 s   内存限制:2 MB [题目描述] Mr.Zero(CH)喜闻乐见地得到了一台内存大大增强 ...

  5. css-test

    transition-content See the Pen NLOgVR by nakata139@gmail.com (@deepblue1982) on CodePen.

  6. Elasticsearch学习(二)————搜索

    Elasticsearch1.query string search1.1.搜索全部// 1. GET http://ip:9200/test/test/_search 结果: { "too ...

  7. mount nfs 各版本之间的转换

    [root@one1-fst-hx ~]# mount.nfs 182.168.2.49:/mnt/sdb/nfs /mnt/nfs2/ nomand,-o vers=3[root@one1-fst- ...

  8. 指针-动态开点&合并线段树

    一个知识点不在一道题里说是没有灵魂的 线段树是用来处理区间信息的咯 但是往往因为需要4倍空间让许多人退却,而动态开点的线段树就非常棒 仿佛只用2倍就可以咯 指针保存位置,即节点信息,是很舒适的,所以用 ...

  9. IFE春季班第一阶段任务(请仔细阅读)

    第一阶段的主要目标是帮助大家 了解.认识.学习.掌握HTML及CSS.第一阶段任务从 3月14日 开始,持续到 4月3日.当然,您也可以在这个时间以后继续自行实践练习. 第一阶段任务一共有 12 个题 ...

  10. [Usaco2009 Nov]lights

    题目描述: 给出$n$,$m$,表示有$n$盏灯和$m$条奇怪的电线,按下电线一段的灯后另一端会有影响. 求最少按几次. 题解: 高消解异或方程组,得到一堆自由元后搜索自由元状态,然后不断更新答案. ...