Tempter of the Bone

来源:互联网 发布:汕头宠物店软件 编辑:程序博客网 时间:2024/05/09 08:17

Tempter of the Bone

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


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
 


 

Author
ZHANG, Zheng
 


 

Source
ZJCPC2004
 


 

Recommend
JGShining

 

 

深搜(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;
反之亦反。

 

#include<iostream>
using namespace std;
char s[10][10];
int s_b,s_e,d_b,d_e;
int vi[10][10];
int move[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int min1;
int n,m,t;
void DFS(int a,int b,int num)
{
 int i;
/* if(a<0||a>=n||b<0||b>=m)
  return ;*/
 if(min1==t)
  return ;
 if(a==d_b&&b==d_e)
 {
  if(num==t)
  {
   min1=num;
   return ;
  }
  else
   return ;
 }
 int temp=abs(a-d_b)+abs(b-d_e); //奇偶剪枝
 temp=t-temp-num;
 if(temp&1||temp<0) return;
 if(num>t)
  return ;
 else
 {
  for(i=0;i<4;i++){
   if((a+move[i][0])>=0&&(a+move[i][0])<n&&(b+move[i][1])>=0&&(b+move[i][1])<m&&s[a+move[i][0]][b+move[i][1]]!='X')
    if(!vi[a+move[i][0]][b+move[i][1]]&&s[a+move[i][0]][b+move[i][1]]!='X'){
     vi[a+move[i][0]][b+move[i][1]]=1;
     DFS(a+move[i][0],b+move[i][1],num+1);
     vi[a+move[i][0]][b+move[i][1]]=0;
    }
  }
 }

}

int main()
{
 
 while(cin>>n>>m>>t&&(m&&n&&t)){
  int i,j;
  min1=100000000;
  memset(vi,0,sizeof(vi));
  for(i=0;i<n;i++)
   for(j=0;j<m;j++)
   {
    cin>>s[i][j];
    if(s[i][j]=='S')
    {
     s_b=i;
     s_e=j;
    }
    if(s[i][j]=='D')
    {
     d_b=i;
     d_e=j;
    }
   }
   
   int num=0;
   vi[s_b][s_e]=1;
   DFS(s_b,s_e,num);
   if(min1!=t)
    cout<<"NO"<<endl;
   else
    cout<<"YES"<<endl;
 }
 return 0;
}