现在的位置: 首页 > 综合 > 正文

HDOJ Tempter of the Bone

2013年10月27日 ⁄ 综合 ⁄ 共 5569字 ⁄ 字号 评论关闭
文章目录

 

Tempter of the Bone

http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=12573&pid=1002

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size:

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

Author

ZHANG, Zheng

Source

ZJCPC2004
 
 
这道题做了N遍还是超时,突然我想到了百度,嘿嘿,百度一下我就知道!!!
下面是大牛的思路和代码:
 
剪枝方法:奇偶剪枝

                             把map看作

                             0 1 0 1 0 1
                             1 0 1 0 1 0
                             0 1 0 1 0 1
                             1 0 1 0 1 0
                             0 1 0 1 0 1

                       从 0->1 需要奇数步

                       从 0->0 需要偶数步
                       那么设所在位置 (x,y) 与 目标位置 (dx,dy)

                       如果abs(x-y)+abs(dx-dy)为偶数,则说明 abs(x-y) 和 abs(dx-dy)的奇偶性相同,需要走偶数步

                       如果abs(x-y)+abs(dx-dy)为奇数,那么说明 abs(x-y) 和 abs(dx-dy)的奇偶性不同,需要走奇数步

                       理解为 abs(si-sj)+abs(di-dj) 的奇偶性就确定了所需要的步数的奇偶性!!

                       而 (ti-setp)表示剩下还需要走的步数,由于题目要求要在 ti时 恰好到达,那么  (ti-step) 与 abs(x-y)+abs(dx-dy) 的奇偶性必须相同

                       因此 temp=ti-step-abs(dx-x)-abs(dy-y) 必然为偶数!

另外,整个图的可以移动步数应该大于指定的时间

代码如下:

#include<iostream>
#include<cmath>
using namespace std;
char map[10][10];
int N,M,T;
int di,dj,escape;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
void dfs(int x,int y,int cnt){
     if(x>N || y>M || x<1 || y<1)
         return ;
     if(cnt==T && x==di && y==dj)
        escape=1;
     if(escape==1)  //这里不能写成  if(cnt==T && x==di && y==dj){
          return ;  //                  escape=1;
                    //                  return;
                    //                  }
     int temp=(T-cnt)-abs(x-di)-abs(y-dj);
     if(temp<0 || temp%2==1)
        return ;
     for(int i=0;i<4;i++){
             if(map[x+dir[i][0]][y+dir[i][1]]!='X'){
                  map[x+dir[i][0]][y+dir[i][1]]='X'; //这里其实可用visited数组来记录,那么下面也就没必
                  dfs(x+dir[i][0],y+dir[i][1],cnt+1); //要恢复了。对于深度搜索不能改变原有的值,但对
                  map[x+dir[i][0]][y+dir[i][1]]='.'; //于广度搜索可以改变
             }
     }
     return ;          
}
int main(){
    while(cin>>N>>M>>T && (N||M||T) ) {
        int wall=0;
        int si,sj;
        for(int i=1;i<=N;i++)
            for(int j=1;j<=M;j++){
                cin>>map[i][j];
                if(map[i][j]=='S'){
                    si=i; sj=j;
                } 
                else if(map[i][j]=='D'){
                    di=i; dj=j;
                }    
                else if(map[i][j]=='X')
                     wall++;                 
            } 
            if(T >= M*N -wall ){
                cout<<"NO"<<endl; 
                continue;  
            }
            map[si][sj]='X';
            escape=0;
            dfs(si,sj,0);
            if(escape==1)
               cout<<"YES"<<endl;
            else
               cout<<"NO"<<endl;                     
    }  
return 0;
}

     
 把其中一个地方改了一下也可以,如下:

 

    

#include<iostream>
#include<cmath>
using namespace std;
char map[10][10];
int N,M,T;
int di,dj,escape;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
void bfs(int x,int y,int cnt){
     if(x>N || y>M || x<1 || y<1)
            return ;
     if(cnt==T && x==di && y==dj){
        escape=1;
       return ;   //在这里返回就必须在下面再返回一次
     }
     int temp=T-cnt-abs(x-di)-abs(y-dj);
     if(temp<0 || temp%2==1)
        return ;
     for(int i=0;i<4;i++){
             if(map[x+dir[i][0]][y+dir[i][1]]!='X'){
                  map[x+dir[i][0]][y+dir[i][1]]='X';
                  bfs(x+dir[i][0],y+dir[i][1],cnt+1);
                  map[x+dir[i][0]][y+dir[i][1]]='.'; 
                  if(escape==1)  return ;    //找到返回                                
             }
     }
     return ;
}
int main(){
    while(cin>>N>>M>>T && ( M|| N || T) ){
           int wall=0;
           int si,sj;
           for(int i=1;i<=N;i++)
              for(int j=1;j<=M;j++){
                  cin>>map[i][j];
                  if(map[i][j]=='S'){
                      si=i;sj=j;
                  }       
                  else if(map[i][j]=='D'){
                      di=i;dj=j;
                  }
                  else if(map[i][j]=='X')
                     wall++;                          
              }
              if(T>=M*N-wall){
                  cout<<"NO"<<endl;
                  continue;               
              }
              map[si][sj]='X';
              escape=0;
              bfs(si,sj,0);      
              if(escape==1)
                  cout<<"YES"<<endl;
              else
                  cout<<"NO"<<endl;            
    }
    return 0;
}

 

 

 

 

 

 

     
     
     
     
     
     
     
     
     
     
    

 

 
 
 

抱歉!评论已关闭.