首页 > 代码库 > hdu 1010 Tempter of the Bone

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): 73870    Accepted Submission(s): 20226


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
NOYES
 

 

Author
ZHANG, Zheng
 

 

Source
ZJCPC2004
 
PS:
DFS,可以用奇偶剪枝,与路径剪枝
 
 1 #include<iostream> 2 #include<queue> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstdlib> 6 using namespace std; 7 int m,n,t; 8 int si,sj,di,dj; 9 char map[10][10];10 bool flag;11 int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};12 13 int border(int x,int y)14 {15     if( (x>=1&&x<=n) && (y>=1&&y<=m) && map[x][y]!=X)16     return 1;17     return 0;18 }19 20 void dfs(int si,int sj,int cnt)21 {22     int i,temp;23     int a,b;24     if(si==di&&sj==dj&&cnt==t)25     {26         flag=1;27         return ;28     }29     temp=(t-cnt)-fabs(si-di)-fabs(sj-dj);//t-cnt为还可以走多少步。30     if(temp<0 || temp%2!=0)//fabs(si-di)-fabs(sj-dj)为当前到终点的最短距离。31     return ;               //不包括对角线,temp小于零的话为步数不足到达终点。32     for(i=0;i<4;i++)       //temp为偶数是因为,你从一固定点到另一个点用3步,33     {                      //但同样也可以用5、7、9步来到达(范围允许下)34         a=si+dir[i][0];35         b=sj+dir[i][1];36         if(border(a,b))37         {38             map[a][b]=X;39             dfs(a,b,cnt+1);40             if(flag)41             return ;42             map[a][b]=.;43         }44     }45     return ;46 }47 48 int main()49 {50     //freopen("in.txt","r",stdin);51     while(cin >> n >> m >>t)52     {53         int count=0;54         if(!n&&!m&&!t)55         break;56         for(int i=1;i<=n;i++)57         {58             for(int j=1;j<=m;j++)59             {60                 cin >> map[i][j];61                 if(map[i][j]==S)62                 {63                     si=i;64                     sj=j;65                 }66                 else if(map[i][j]==D)67                 {68                     di=i;69                     dj=j;70                 }71                 else if(map[i][j]==X)72                 {73                     count++;74                 }75             }76         }77         if(n*m-count<=t)//如果max-墙的步数小于等于规定步数78         {               //说明全走完也到不了79             printf("NO\n");80             continue;81         }82         flag=0;83         map[si][sj]=X;84         dfs(si,sj,0);85         if(flag)86         printf("YES\n");87         else88         printf("NO\n");89 90     }91     return 0;92 }
View Code

 

hdu 1010 Tempter of the Bone