HDU 1010 (Tempter of the Bone)

来源:互联网 发布:大数据信息化时代 编辑:程序博客网 时间:2024/06/16 06:35

Tempter of the Bone


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
//具体的有关奇偶剪枝的概念可以参考:点击打开链接
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;char a[8][8];int visit[8][8];bool ans;int n,m,t;void dfs(int x,int y,int T){if(a[x][y]=='D'){if(T==t)               //如果搜索到了终点,则把ans的值记为真;    ans=true;return ;}if(T==t)return;                //如果到了t秒仍然没有搜索到终点,则返回空; if(ans==false){if(x+1<n  &&visit[x+1][y]==0)          //在ans为假的情况下进行深度搜索; {visit[x][y]=1;dfs(x+1 ,y ,T+1);visit[x][y]=0;}if(x-1>=0  &&visit[x-1][y]==0){visit[x][y]=1;dfs(x-1 ,y ,T+1);visit[x][y]=0;}if(y+1<m  &&visit[x][y+1]==0){visit[x][y]=1;dfs(x,y+1,T+1);visit[x][y]=0;}if(y-1>=0  &&visit[x][y-1]==0){visit[x][y]=1;dfs(x,y-1,T+1);visit[x][y]=0;}}}int main(){int si,sj,ei,ej;while(scanf("%d%d%d",&n,&m,&t),n||m||t){memset(visit,0,sizeof(visit));     //初始化标记数组为0,后边二维数组中为'.'的元素都被标记为0 for(int i=0;i<n;i++)scanf("%s",a[i]);                    //按行输入二维数组元素 for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(a[i][j]=='X')   visit[i][j]=1;            //如果迷宫中的这个位置上是墙的话,则把其标记为1; else if(a[i][j]=='D')  {      ei=i; ej=j;            //如果这个位置是终点的话,则把它的横纵坐标值记下来;       }    else if(a[i][j]=='S')      {                  si=i; sj=j;              //如果这个位置是起点的话,则把它的横纵坐标也记下来;   }}}ans= false;                   //记一个布尔类型的元素来表示答案;     int k= (ei-si+ej-sj)&1;       //奇偶剪枝,如果最短路径和迷宫出口开启的时间同奇同偶的话才可能满足题目要求;     int l=t&1;      if(n&&m&&t&&k==l)         dfs(si,sj,0);                if(ans)      printf("YES\n");    else      printf("NO\n");}return 0;}


原创粉丝点击