hdu 1010 Tempter of the Bone

来源:互联网 发布:mac如何复制文字 编辑:程序博客网 时间:2024/06/08 11:47

Tempter of the Bone

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


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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 


 

Sample Output
NOYES
 


这道题说 是否可以在第t步恰好走到规定的终点,已经走过的路不可以再走。 很明显是 DFS+奇偶剪枝

奇偶剪枝
编辑

 

 

目 录

1描述

2结论

3原理补充

 

 

 

1描述

奇偶剪枝是数据结构的搜索中,剪枝的一种特殊小技巧。
现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,
s
    
|
    
|
    
|
    
+
e
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;
s
  
+
 
|
+
   
|
    
+
e
如图,为一般情况下非最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);

2结论

推广之,若 t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达;
返回,false;
反之亦反。

3原理补充

鉴于很多同学对奇偶剪枝根本原理的兴趣,所以hj决定再补充一下本词条。
还是以这个为例子吧,现在我把矩阵填满 0 和 1 [1]
0
1
0
1
01
0
1
0
1
0
1
010
1
0101
0
1
0
1
0
我们现假设从 0 开始走,则不难证明,
从任意 0 走到任意 1 始终是奇数步;
从任意 0 走到任意 0 始终是偶数步;
引用描述里的“例子”, s 到 e 的最短步数为 t (当然你也可以理解成此时到终点刚好剩余 t 步等等)。
则,我们从 s 到 e 的步数之和(或者说总距离)总可以表示成 sum= t + extra ( extra>=0 ),其中 extra 表示额外的步数。[2]
比如“例子”里面的,做例1吧
s
  
+
 
|
+
   
|
    
+
e
此时 t=8,sum=14,所以我们容易得到 extra=6。也就是说按照这个走法,需要在最短的步数上再走额外的 6 步(先不用太在意这些偏移是在什么地方产生的)。
在来一个例2吧,
s
  
+
 
|
+
   
|
 +—e
+
  
此时,t=7,sum=15,所以我们也容易得到 extra=8。
根据理科生的天性,由这两个一般性的例子,我们很容易嗅察到 extra 都为偶数。先带着疑惑,
再来看我给的 0 、1 矩阵。
0
1
0
1
01
0
1
0
1
0
1
010
1
0101
0
1
0
1
0
设左上角坐标为(1,1),右下角坐标为(5,5).
那么我们给的例1,
起点 s 的坐标为(1,1),此点为“0”;
终点 e 为(5,5),此点为“0”。
所以t=8,为偶数。
现在我们再倒过来看,从终点(也就是 e )出发,把最短步数 t=8 耗费掉,不妨这样走,
s
   +   
+
—   |   +—   
e
如图所示从 e (5,5)耗费 8 步走到了(1,5)点。
因为是从 0 走偶数步,所以走到的坐标也一定是 0 ,就像这里的(1,5)点是 0 一样。
又因为最短步数已经耗费掉了,所以不管怎样,从(1,5)再走回到起点 s 所用的步数总是最开始从起点 s 走到终点 e 所花的某一个额外步数 extra 。
注意到,(1,5)点和起点 s (1,1)都是 0,也就是说,这个 extra 必然是偶数!
再看例2,同样从终点 e 开始耗费 t=7 步,
则所到的点一定是 0 (不管她在哪里),再从这个点回到起点 s ,所用的 extra 也必然是个偶数!
所以无论如何,sum= t + extra ( extra>=0 ) 中的 extra 都是一个偶数
那么我们就可以用公式 t-[abs(ex-sx)+abs(ey-sy)] 计算出extra是否为偶数来判断当前点能否恰好在这么多步到达终点了。
有的同学可能会说以 1 为 起点呢。。其实是一样的啦,自己去捣鼓吧。
现在明白了么。。那么我再给个经典的实例

 

#include<iostream>#include<algorithm>using namespace std;int n,m,t;int sx,sy,gx,gy;char str[8][8];bool flag;int dir[4][2]={1,0,-1,0,0,1,0,-1};void dfs(int x,int y,int time){if(flag) return ;if(x==gx && y==gy && time==t) { flag=1; return ;}//true;//if( (abs(x-gx)+abs(y-gy)-(t-time))%2!=0 ) return;int tx,ty,i;for(i=0;i<4;i++){tx=x+dir[i][0];ty=y+dir[i][1];//if( (abs(tx-gx)+abs(ty-gy))%2==(t-time-1)%2      )//if(tx>=0 && tx<n && ty>=0 && ty<m && str[tx][ty]!='X')//if( (abs(tx-gx)+abs(ty-gy)-(t-time-1))%2==0 ){str[tx][ty]='X';dfs(tx,ty,time+1);str[tx][ty]='.';}}}//int main(){int i,j;while (cin>>n>>m>>t){if(n==0 && m==0 && t==0) break;for(i=0;i<n;i++){for(j=0;j<m;j++){cin>>str[i][j];if(str[i][j]=='S'){sx=i;sy=j;}else if(str[i][j]=='D'){gx=i;gy=j;}}}str[sx][sy]='X';flag=false;dfs(sx,sy,0);if(!flag) cout<<"NO";else cout<<"YES";cout<<endl;}return 0;}


 

 

下面这个是参考别人的。。思路  不过貌似这道题只要用到奇偶剪枝就可以了

 

 

 

#include<iostream>#include<algorithm>using namespace std;int n,m,t;//n行 m列char str[8][8];int sx,sy,ex,ey,flag,cstep,remain;int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};void dfs(int x,int y,int step){if(flag) return ;if(x==ex && y==ey && step==t)  {flag=1; return;}int tx,ty;if(  (   abs(x-ex)+abs(y-ey)  )%2 != ( t-step)  %2 ) return;//if(  (   abs(x-ex)+abs(y-ey)  )  & 1 != ( t-step)  &  1 ) return;for(int i=0;i<4;i++){tx=x+dir[i][0];ty=y+dir[i][1];if(tx>=0 && tx<n && ty>=0 && ty<m && str[tx][ty]!='X'){str[tx][ty]='X';dfs(tx,ty,step+1);str[tx][ty]='.';}}}int main(){int i,j;while(cin>>n>>m>>t){if(n==0 && m==0 && t==0) break;flag=0;remain=0;for(i=0;i<n;i++){for(j=0;j<m;j++){cin>>str[i][j];if(str[i][j]=='S') {sx=i; sy=j;}else if(str[i][j]=='D'){  ex=i;ey=j; ++remain; }else if(str[i][j]=='.') ++remain;}}str[sx][sy]='X';if(remain>=t)  dfs(sx,sy,0);if(flag) cout<<"YES"<<endl;else cout<<"NO"<<endl;}return 0;}


 

 

 

 

原创粉丝点击