Tempter of the Bone(dfs奇偶剪枝)

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:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #define mem(x,y) memset(x,0,sizeof(x))
 5 const int MAXN=7;
 6 int disx[4]={0,0,1,-1};
 7 int disy[4]={1,-1,0,0};
 8 int N,M,T,e_x,e_y;
 9 char map[MAXN][MAXN];
10 int vis[MAXN][MAXN];
11 int ans;
12 void dfs(int x,int y,int sec){
13     if(ans)return;
14     if(map[x][y]==‘D‘){
15     //    printf("%d**",sec);
16         if(sec==T)ans=1;
17         return;
18     }
19     int temp=T-abs(e_x-x)-abs(e_y-y)-sec;//奇偶剪枝。。。
20         if(temp<0||temp&1)return;
21
22     for(int i=0;i<4;i++){
23         int nx,ny;
24         nx=x+disx[i];ny=y+disy[i];
25         if(nx<0||ny<0||nx>=N||ny>=M||vis[nx][ny]||map[nx][ny]==‘X‘)continue;//>=写错了,错了半天。。。
26         if(sec+1>T)continue;
27         vis[nx][ny]=1;
28         dfs(nx,ny,sec+1);
29         vis[nx][ny]=0;
30     }
31 }
32 int main(){
33     while(scanf("%d%d%d",&N,&M,&T),N||M||T){
34         for(int i=0;i<N;i++)scanf("%s",map[i]);
35         int sx,sy;
36         int wall=0;
37         for(int x=0;x<N;x++)for(int y=0;y<M;y++)
38         if(map[x][y]==‘S‘)sx=x,sy=y;
39         else if(map[x][y]==‘D‘)e_x=x,e_y=y;
40         else if(map[x][y]==‘X‘)wall++;
41         mem(vis,0);vis[sx][sy]=1;
42         ans=0;
43         if(T<N*M-wall)dfs(sx,sy,0);
44         if(ans)puts("YES");
45         else puts("NO");
46     }
47     return 0;
48 }

bfs  wa代码扔着留念吧:

#include<stdio.h>
#include<string.h>
const int MAXN=7;
#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[4]={0,0,1,-1};
int disy[4]={1,-1,0,0};
int vis[MAXN][MAXN];
int N,M,T;
bool bfs(int sx,int sy){
    queue<Node>dl;
    Node a,b;
    mem(vis,0);
    vis[sx][sy]=1;
    a.x=sx;a.y=sy;a.sec=0;
    dl.push(a);
    while(!dl.empty()){
        a=dl.front();
        dl.pop();
        for(int i=0;i<4;i++){
            b.x=a.x+disx[i];b.y=a.y+disy[i];b.sec=a.sec+1;
            if(b.x<0||b.y<0||b.x>=N||b.y>=M||vis[b.x][b.y]==1||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]=1;
            dl.push(b);
        }
    }
    return false;
}
int main(){
    while(scanf("%d%d%d",&N,&M,&T),N|M|T){
        for(int i=0;i<N;i++)scanf("%s",map[i]);
        int sx,sy;
        for(int x=0;x<N;x++)for(int y=0;y<N;y++)
        if(map[x][y]==‘S‘)sx=x,sy=y;
        if(bfs(sx,sy))puts("YES");
        else puts("NO");
    }
    return 0;
}
时间: 2024-10-03 00:11:01

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

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

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 75141    Accepted Submission(s): 20531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

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

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 82702    Accepted Submission(s): 22531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu 1010 Tempter of the Bone DFS+奇偶剪枝,入门题

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 78390    Accepted Submission(s): 21395 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of Bone DFS + 奇偶剪枝

奇偶剪枝: 对于从起始点 s 到达 终点 e,走且只走 t 步的可达性问题的一种剪枝策略. 如下列矩阵 : 从任意 0 到达 任意 0,所需步数均为偶数,到达任意 1 ,均为奇数.反之亦然 所以有,若s走且只走 t 步可到达e,则必有t >= abs(s.x - e.x) + abs(s.y - e.y),且 (t&1) == ((abs(s.x - e.x) + abs(s.y - e.y))&1). 否则必不可达. #include <iostream> #inclu

Tempter of the Bone (奇偶剪枝?DFS)

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 despe

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

题目链接:点击打开链接 题目描述:给定一个迷宫,给一个起点和一个终点,问能否恰好经过T步到达终点?每个格子不能重复走 解题思路:dfs+剪枝 剪枝1:奇偶剪枝,判断终点和起点的距离与T的奇偶性是否一致,如果不一致,直接剪掉 剪枝2:如果从当前到终点的至少需要的步数nt加上已经走过的步数ct大于T,即nt+ct>t剪掉 剪枝3:如果迷宫中可以走的格子小于T直接剪掉 启发:剪枝的重要性 代码: #include <cstdio> #include <cstdlib> #inclu

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

http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心外,还需要强力的剪枝. 奇偶性剪枝:参考 http://www.cppblog.com/Geek/archive/2010/04/26/113615.html 1 #include <iostream> 2 #include <cstring> 3 #include <cstdi

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c

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

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 71121    Accepted Submission(s): 19592 Problem Description The doggie found a bone in an ancient maze, which fascinated him a