码迷,mamicode.com
首页 > 其他好文 > 详细

Tempter of the Bone(dfs奇偶剪枝)

时间:2015-10-07 22:45:07      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

 

Tempter of the Bone(dfs奇偶剪枝)

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4859548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!