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. linux printf和fork()问题小结

    总结如下: printf("father begin"); pid_t pid; pid = fork(); ) { ) { printf("father out&quo ...

  2. ANDROID SHAPE画圆形背景_ANDROID实现角标布局

    ANDROID SHAPE画圆形背景_ANDROID实现角标布局 <?xml version="1.0" encoding="UTF-8"?> &l ...

  3. 【转】Python3.x移除了callable内建函数

    原文地址:http://www.cnblogs.com/elvisxu/archive/2010/10/26/1861958.html 最近学习Python的时候,在Python3下跑<Dive ...

  4. 前端工程师须知pc电脑端分辨率

    PC端 按屏幕宽度大小排序(主流的用橙色标明) 分辨率   比例 | 设备尺寸 1024*500 (8.9寸) 1024*768 (比例4:3  | 10.4寸.12.1寸.14.1寸.15寸; ) ...

  5. Android在应用中依据包名启动另外一个APP

    以下为TestIntentData工程 MainActivity如下: package cn.testintentdata; import java.util.List; import android ...

  6. 【MongoDB数据库】MongoDB 命令入门初探

    MongoDB是一款NoSql数据库,使用了"面向集合"(Collection-Oriented)原理,意思是数据被分组存储在数据集中,被称为一个集合(Collection).每一 ...

  7. Python之lxml

    作者:Shane 出处:http://bluescorpio.cnblogs.com lxml takes all the pain out of XML. Stephan Richter lxml是 ...

  8. SurfaceView 和 View 区别

    android.view.View 和 android.view.SurfaceView SurfaceView 是从 View 基类中派生出来的显示类,直接子类有 GLSurfaceView和Vid ...

  9. 侯老师的话(Application Framework)

    摘自http://blog.csdn.net/zlc19876/article/details/5355022 本篇文章主要介绍了"侯老师的话(Application Framework)& ...

  10. 虚拟键盘,移动web开发的痛

    原生APP的弹出虚拟键盘和收回虚拟键盘,输入框始终贴在虚拟键盘的上方.如下图: 如果移动端web也想做类似的功能,可以实现吗?  你可能会说着:“不就是放一个position: fixed;的输入框在 ...