题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1010

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 69549    Accepted Submission(s): 19126

Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.



The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the
T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for
more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
 
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the
maze layout, with each line containing M characters. A character is one of the following:



'X': a block of wall, which the doggie cannot enter; 

'S': the start point of the doggie; 

'D': the Door; or

'.': an empty block.



The input is terminated with three 0's. This test case is not to be processed.
 
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
 
Sample Output
NO
YES
 

题目大意:S起点 D终点 X障碍物 .可走路  输入N,M,T 表示N行 M列  问你是否能在 T 时刻恰好到达终点D。(注意这里是刚刚好T步 多了少了都不行 而且不能重复走)

思路1:与上一题一样   hdu1242
Rescue
 一样  记录所有能到终点的路径到时候找是否存在刚刚好一个长度正好为T的

注意:此思路的代码还能优化 读者有兴趣的可以试试

#include<iostream>
using namespace std;
#include<string.h>
#define max 205
char map[max][max];
long a[100000],step,sum,n,m,visited[max][max];
long directions[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void DFS(int x,int y)
{
int i,mx,my; if(map[x][y]=='D')
a[sum++]=step; else if(map[x][y]!='X')
{ for(i=0;i<4;i++)
{
mx=x+directions[i][0];
my=y+directions[i][1]; if(map[mx][my]!='X'&&mx>=1&&mx<=n&&my>=1&&my<=m&&!visited[mx][my])//不是墙并且没走过
{
// if(map[x][y]=='x')
// step++;
step++;
visited[mx][my]=1; DFS(mx,my); //所以关键要得到值的是递归的这一步 推导的时候让这个的下一个DFS就是到达朋友那点比较好理解为什么后面要还原 visited[mx][my]=0;//这一步的原因,上面DFS进行完后要将状态还原
step--; //下面这些都要还原
// if(map[x][y]=='x')
// step--; }
}
}
} int main()
{ long i,j,x,y,min;
int kk;
while(cin>>n>>m>>kk)
{
if(n==0&&m==0&&kk==0)break;
memset(visited,0,sizeof(visited));
sum=0;
step=0;
min=max; for(i=1;i<=n;i++)
{ for(j=1;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')//记录天使的地址
{
x=i;
y=j;
}
}
} visited[x][y]=1;
DFS(x,y); int flag=0;
for(i=0;i<sum;i++)
if(a[i]==kk){flag=1;break;}
if(flag==1)cout<<"YES"<<endl;
else
cout<<"NO"<<endl; }
return 0;
}

思路2:

借用这里的,

奇偶剪枝:
是数据结构的搜索中剪枝的一种特殊小技巧。
现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,
 
s        
|        
|        
|        
+ e
 
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;
  
s  
  +  
| +      
|        
+ e
 
如图,为一般情况下非最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);
故,若t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;
返回,false;
反之亦反。




280kb,156ms;


#include<iostream>
#include<cstring>
#define N 10
using namespace std; int n,m,t,end_i,end_j;
bool visited[N][N],flag,ans;
char map[N][N];
int a[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int abs(int a,int b)//用cmath也可以
{//
if(a<b) return b-a;//
else return a-b;//
}//
void DFS(int i,int j,int c)
{
if(flag) return ;
if(c>t) return ;
if(i<0||i>=n||j<0||j>=m) {return ;}
if(map[i][j]=='D'&&c==t) {flag=ans=true; return ;}
int temp=abs(i-end_i)+abs(j-end_j);
temp=t-temp-c; //t扣掉还要走的最短步temp 和 已经走过的 c 如果这些步还是奇数直接不满足
if(temp&1) return ;//奇偶剪枝 奇数return
for(int k=0;k<4;k++)
if(!visited[i+a[k][0]][j+a[k][1]]&&map[i+a[k][0]][j+a[k][1]]!='X') //开始进行各个方向的探索 记得回溯,取消之前走的状态
{
visited[i+a[k][0]][j+a[k][1]]=true;
DFS(i+a[k][0],j+a[k][1],c+1);
visited[i+a[k][0]][j+a[k][1]]=false;
}
} int main()
{
int i,j,x,y,k;
while(cin>>m>>n>>t&&(m||n||t))
{
memset(visited,false,sizeof(visited));
k=0;//记录障碍物的数量
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')
{
x=i;y=j;
visited[i][j]=true;
}
if(map[i][j]=='D')
{
end_i=i;end_j=j;
}
if(map[i][j]=='X')k++;
}
}
ans=flag=false;
if(n*m-k-1>=t) DFS(x,y,0);
if(ans) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

与上面的思路一样,代码写长了 不过时间变短了 也就是那个for(0->3)这里不一样

280 KB 78 ms

#include<iostream>
#include<cstring>
#define N 10 using namespace std; int n,m,t,end_i,end_j;
bool visited[N][N],flag,ans;
char map[N][N]; int abs(int a,int b)
{
if(a<b) return b-a;
else return a-b;
} void DFS(int i,int j,int c)
{
if(flag) return ;
if(c>t) return ;
if(i<0||i>=n||j<0||j>=m) {return ;}
if(map[i][j]=='D'&&c==t) {flag=ans=true; return ;}
int temp=abs(i-end_i)+abs(j-end_j);
temp=t-temp-c;
if(temp&1) return ;//奇偶剪枝 if(!visited[i-1][j]&&map[i-1][j]!='X')
{
visited[i-1][j]=true;
DFS(i-1,j,c+1);
visited[i-1][j]=false;
}
if(!visited[i+1][j]&&map[i+1][j]!='X')
{
visited[i+1][j]=true;
DFS(i+1,j,c+1);
visited[i+1][j]=false;
}
if(!visited[i][j-1]&&map[i][j-1]!='X')
{
visited[i][j-1]=true;
DFS(i,j-1,c+1);
visited[i][j-1]=false;
}
if(!visited[i][j+1]&&map[i][j+1]!='X')
{
visited[i][j+1]=true;
DFS(i,j+1,c+1);
visited[i][j+1]=false;
}
} int main()
{
int i,j,x,y,k;
while(cin>>m>>n>>t&&(m||n||t))
{
memset(visited,false,sizeof(visited));
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')
{
x=i;y=j;
visited[i][j]=true;
}
if(map[i][j]=='D')
{
end_i=i;end_j=j;
}
if(map[i][j]=='X')k++;
}
}
ans=flag=false;
if(n*m-k-1>=t) DFS(x,y,0);
if(ans) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu1010--Tempter of the Bone(迷宫)的更多相关文章

  1. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  2. hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  3. Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. hdu1010 Tempter of the Bone(深搜+剪枝问题)

    Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ...

  5. HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】

    Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  6. HDU1010 Tempter of the Bone(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...

  7. HDU1010 Tempter of the Bone

    解题思路:相当经典的一题,回溯,具体细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> ...

  8. HDU1010 --- Tempter of the Bone(dfs+剪枝)

    小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里.经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t ...

  9. ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

    题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径 ...

  10. ZOJ2110 HDU1010 搜索 Tempter of the Bone

    传送门:Tempter of the Bone 大意是给一个矩阵,叫你是否可以在给定的可走路径上不重复地走,在最后一秒走到终点. 我用了两个剪枝,且称其为简直001和剪枝002,事实证明001不要都可 ...

随机推荐

  1. javascript json对象操作(基本增删改查)

    /** * Json对象操作,增删改查 * * @author lellansin * @blog www.lellansin.com * @version 0.1 * * 解决一些常见的问题 * g ...

  2. Windows8系统下设置Mongodb开机启动

    1. 官网下载安装 MongoDB https://www.mongodb.com/ 2. 环境变量设置 把 mongod.exe 所在路径加入到环境变量的PATH, 我这里安装的路径是 D:\db\ ...

  3. Shellinabox on centos6.9

    介绍 一款实用的web linux终端, 并且保证操作安全性(屏蔽root用户) 下面以centos6.9为例 安装 首先安装epel仓库,再安装shellinabox yum -y install ...

  4. 关于使用READ TABLE语句

    READ tabe 是用来遍历内表,取第一条符合条件的记录. READ TABLE <itab> [INTO <wa>] WITH KEY <key> [BINAR ...

  5. 20155222卢梓杰 《Java程序设计》第1周学习总结

    20155222 <Java程序设计>第1周学习总结 教材学习内容总结 JDK是一个工具程序,包括了JAVA程序语言,工具程序与JRE,JRE包括了部署技术,JAVA SE API 与 J ...

  6. 多级反馈序列c语言模拟实现

    多级反馈队列调度算法: 1.设置多个就绪队列,并给队列赋予不同的优先级数,第一个最高,依次递减. 2.赋予各个队列中进程执行时间片的大小,优先级越高的队列,时间片越小. 3.当一个新进程进入内存后,首 ...

  7. 20145207 2016-2017-2 《Java程序设计》第4周学习总结

    一.继承与多态 1.继承的定义 面对对象中,子类继承父类,避免重复的行为定义,不过并非为了避免重复定义行为就使用继承,滥用而继承会导致程序维护上的问题. 程序代码重复在程序设计上就是不好的信号,多个类 ...

  8. 20145226夏艺华 《Java程序设计》实验报告一

    实验一 Java开发环境的熟悉(Linux + IDEA) 实验内容 使用JDK编译.运行简单的Java程序: 使用Eclipse 编辑.编译.运行.调试Java程序. 实验步骤 (一)命令行下Jav ...

  9. Discover Feature Engineering, How to Engineer Features and How to Get Good at It

    Feature engineering is an informal topic, but one that is absolutely known and agreed to be key to s ...

  10. 【转】 mysql使用federated引擎实现远程访问数据库(跨网络同时操作两个数据库中的表)

    原文转自:http://www.2cto.com/database/201412/358397.html 问题: 这里假设我需要在IP1上的database1上访问IP2的database数据库内的t ...