Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 92175    Accepted Submission(s): 25051

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
 题解:
刚看到这个题用了bfs果断wa,想了想恰好到达,应该是dfs,看了康总的,还有奇偶剪枝,改了还是超时,找了半天发现中间少了两个等号。。。。
奇偶剪枝:
是数据结构的搜索中,剪枝的一种特殊小技巧。
现假设起点为(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;
反之亦反。
 dfs:
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define mem(x,y) memset(x,0,sizeof(x))
const int MAXN=;
int disx[]={,,,-};
int disy[]={,-,,};
int N,M,T,e_x,e_y;
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int ans;
void dfs(int x,int y,int sec){
if(ans)return;
if(map[x][y]=='D'){
// printf("%d**",sec);
if(sec==T)ans=;
return;
}
int temp=T-abs(e_x-x)-abs(e_y-y)-sec;//奇偶剪枝。。。
if(temp<||temp&)return; for(int i=;i<;i++){
int nx,ny;
nx=x+disx[i];ny=y+disy[i];
if(nx<||ny<||nx>=N||ny>=M||vis[nx][ny]||map[nx][ny]=='X')continue;//>=写错了,错了半天。。。
if(sec+>T)continue;
vis[nx][ny]=;
dfs(nx,ny,sec+);
vis[nx][ny]=;
}
}
int main(){
while(scanf("%d%d%d",&N,&M,&T),N||M||T){
for(int i=;i<N;i++)scanf("%s",map[i]);
int sx,sy;
int wall=;
for(int x=;x<N;x++)for(int y=;y<M;y++)
if(map[x][y]=='S')sx=x,sy=y;
else if(map[x][y]=='D')e_x=x,e_y=y;
else if(map[x][y]=='X')wall++;
mem(vis,);vis[sx][sy]=;
ans=;
if(T<N*M-wall)dfs(sx,sy,);
if(ans)puts("YES");
else puts("NO");
}
return ;
}

bfs  wa代码扔着留念吧:

#include<stdio.h>
#include<string.h>
const int MAXN=;
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
struct Node{
int x,y,sec;
};
char map[MAXN][MAXN];
int disx[]={,,,-};
int disy[]={,-,,};
int vis[MAXN][MAXN];
int N,M,T;
bool bfs(int sx,int sy){
queue<Node>dl;
Node a,b;
mem(vis,);
vis[sx][sy]=;
a.x=sx;a.y=sy;a.sec=;
dl.push(a);
while(!dl.empty()){
a=dl.front();
dl.pop();
for(int i=;i<;i++){
b.x=a.x+disx[i];b.y=a.y+disy[i];b.sec=a.sec+;
if(b.x<||b.y<||b.x>=N||b.y>=M||vis[b.x][b.y]==||map[b.x][b.y]=='X')continue;
if(b.sec>T)continue;
if(map[b.x][b.y]=='D'){
if(b.sec==T)return true;
continue;
}
vis[b.x][b.y]=;
dl.push(b);
}
}
return false;
}
int main(){
while(scanf("%d%d%d",&N,&M,&T),N|M|T){
for(int i=;i<N;i++)scanf("%s",map[i]);
int sx,sy;
for(int x=;x<N;x++)for(int y=;y<N;y++)
if(map[x][y]=='S')sx=x,sy=y;
if(bfs(sx,sy))puts("YES");
else puts("NO");
}
return ;
}

Tempter of the Bone(dfs奇偶剪枝)的更多相关文章

  1. hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

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

  2. M - Tempter of the Bone(DFS,奇偶剪枝)

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

  3. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

  4. 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 ...

  5. hdu Tempter of the Bone (奇偶剪枝)

    学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...

  6. hdu1010Tempter of the Bone(dfs+奇偶剪枝)

    题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...

  7. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

  8. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  9. hdoj--1010<dfs+奇偶剪枝>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...

随机推荐

  1. ubuntu远程windows服务器

    ubuntu端: sudo apt-get install rdesktop windows端: 需要允许此windows远程访问.我的windows是windows server2012,基本操作: ...

  2. 走进C标准库(2)——"stdio.h"中的fopen函数

    其他的库文件看起来没有什么实现层面的知识可以探究的,所以,直接来看stdio.h. 1.茶余饭后的杂谈,有趣的历史 在过去的几十年中,独立于设备的输入输出模型得到了飞速的发展,标准C从这个改善的模型中 ...

  3. html---id,name和value

    id是唯一标识符,不允许有重复值(类似数据表的主键,pk),可以通过它的值来获得对应的html标签对象.(如果在同一页面代码中,出现重复的id,会导致不可预料的错误) js代码可通过document. ...

  4. Cortex-M3学习日志(五) -- DAC实验

    终于逮了个忙里偷闲的机会,就再学一下LPC1768的外围功能吧,循序渐进是学习的基本规则,也许LPC1768的DAC与8位单片机16位单片机里面集成的DAC操作类似,但是既然这是懒猫的学习日志,就顺便 ...

  5. CSliderCtrl鼠标点击精确定位

    实现CSliderCtrl的子类CXXCtrl 响应左键按下消息 ON_WM_LBUTTONDOWN() void CXXCtrl::OnLButtonDown(UINT nFlags, CPoint ...

  6. iOS6和iOS7代码的适配(4)——tableView

    iOS7上不少控件的样子有了变化(毕竟要扁平化嘛),不过感觉变化最大的肯定非tableView莫属.因为这个控件的高度可定制性,原先是使用及其广泛的,这样的一个改变自然也影响颇大. 1.accesso ...

  7. 在OC项目工程中混编Swift

    1.创建一个OC项目工程,然后在Build Settings中找到如下字段,修改. 2.然后在项目中创建swift文件,如果系统提示是否需要创建桥接文件的时候,点击确定. 然后在Build Setti ...

  8. ToolStripMenuItem控件实现DatagridView行的上下移

    /*--------------行上移------------------*/ 1 private void 上移ToolStripMenuItem_Click(object sender, Even ...

  9. win7(32 bit) + IE8 环境,IE8无法弹窗(错误提示:“此网页上的错误可能会使它无法正确运行”),有关的系统注册信息损坏——解决方法

    错误截图如下:   IE有关的系统注册信息损坏,导致IE无法正常弹窗.   解决办法:重新注册与IE有关的DLL文件,具体如下: 1.以管理员身份运行附件脚本(新建txt文件,将下面代码复制到txt文 ...

  10. D - Common Subsequence

    D - Common Subsequence Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I ...