hdu 1010 Tempter of the Bone

Tempter of the Bone

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


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

 

  1 /*
  2 总结:
  3 一、优化条件很多,最好多找几个。这里用到的:
  4     1.能走的路比时间少肯定不行
  5     2.时间的奇偶性决定时间差的奇偶性
  6 二、坐标要搞清楚,这题错了很久都是因为N和M错了的
  7 */
  8 #include<iostream>
  9 #include<algorithm>
 10 #include<cstdio>
 11 #include<queue>
 12 #include<cstring>
 13 #include<cmath>
 14 using namespace std;
 15 int N,M,T;
 16 char map[10][10];
 17 int sx,sy,dx,dy;
 18 bool done;
 19 void dfs(int x,int y,int t){
 20     //cout<<"now"<<x<<ends<<y<<ends<<t<<ends<<dx<<ends<<dy<<ends<<T<<endl;
 21     if(x==dx&&y==dy&&t==T){
 22         //cout<<"done"<<endl;
 23         done=true;
 24         return ;
 25     }
 26     if(done)
 27     return;
 28     //down
 29     if(y<M-1&&map[x][y+1]!='X'){
 30         //cout<<"down"<<endl;
 31         map[x][y+1]='X';
 32         dfs(x,y+1,t+1);
 33         map[x][y+1]='.';
 34     }
 35     //right
 36     if(x<N-1&&map[x+1][y]!='X'){
 37         //cout<<"right"<<endl;
 38         map[x+1][y]='X';
 39         dfs(x+1,y,t+1);
 40         map[x+1][y]='.';
 41     }
 42     //up
 43     if(y>0&&map[x][y-1]!='X'){
 44         //cout<<"up"<<endl;
 45         map[x][y-1]='X';
 46         dfs(x,y-1,t+1);
 47         map[x][y-1]='.';
 48     }
 49 
 50     //left
 51     if(x>0&&map[x-1][y]!='X'){
 52         //cout<<"left"<<endl;
 53         map[x-1][y]='X';
 54         dfs(x-1,y,t+1);
 55         map[x-1][y]='.';
 56     }
 57 
 58     return ;
 59 }
 60 int main()
 61 {
 62     while(scanf("%d %d %d",&N,&M,&T)==3&&N){
 63         int wall=0;
 64         done=false;
 65         for(int i=0;i<N;i++){
 66             for(int j=0;j<M;j++){
 67                 cin>>map[i][j];
 68                 if(map[i][j]=='S'){
 69                     sx=i;
 70                     sy=j;
 71                 }
 72                 if(map[i][j]=='D'){
 73                     dx=i;
 74                     dy=j;
 75                 }
 76                 if(map[i][j]=='X'){
 77                     wall++;
 78                 }
 79             }
 80         }
 81         if(N*M-wall<=T)
 82         {
 83             cout<<"NO"<<endl;
 84             continue;
 85         }
 86         if((((sx+sy)%2-(dx+dy)%2)==0)&&T%2==1)
 87         {
 88             //cout<<si<<ends<<sj<<ends<<di<<ends<<dj<<endl;
 89             cout<<"NO"<<endl;
 90             continue;
 91         }
 92         if(abs((sx+sy)%2-(dx+dy)%2)==1&&T%2==0)
 93         {
 94             cout<<"NO"<<endl;
 95             continue;
 96         }
 97         else{
 98             map[sx][sy]='X';
 99             dfs(sx,sy,0);
100             cout<<(done?"YES":"NO")<<endl;
101         }
102     }
103     return 0;
104 }

 

posted @ 2013-12-05 17:03  mrbean  阅读(186)  评论(0编辑  收藏  举报