1. 程式人生 > 其它 >hduoj---Tempter of the Bone

hduoj---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): 55812    Accepted Submission(s): 15052

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

Author

ZHANG, Zheng

Source

ZJCPC2004

搜尋題:dfs深度優先搜尋、、、、

程式碼:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<math.h>
  4 int n,m,ex,ey,t;
  5 bool success;
  6 char maze[10][10];   //考慮到要加邊
  7 /*
  8  stx ---->開始x座標
  9  sty ---->開始y座標
 10  dt  ---->花掉時間
 11 */
 12 
 13 void dfs(int stx,int sty,int dt )
 14 {
 15   /*
 16     超過規定時間,超邊都表示無法完成任務
 17   */
 18     if(stx<=0||stx>n||sty<=0||sty>m)      /* 可以加邊處理的*/ 
 19                    return ;
 20     if(stx==ex&&sty==ey&&dt==t)
 21             success=true;
 22     if(success) return ;
 23    int temp=(t-dt)-abs(ex-stx)-abs(ey-sty);
 24     if(temp<0||temp&1)   //奇偶剪枝
 25         return ;
 26 /*
 27    0 1 0 1 0 1 0 1 0
 28    1 0 1 0 1 0 1 0 1
 29    0 1 0 1 0 1 0 1 0
 30    1 0 1 0 1 0 1 0 1
 31    0 1 0 1 0 1 0 1 0
 32    1 0 1 0 1 0 1 0 1
 33    0 1 0 1 0 1 0 1 0
 34    1 0 1 0 1 0 1 0 1 
 35  無論是從o 開始還是從1開始,
 36  都是 0--->1 或者1--->0 都是奇數步
 37   0-->0 , 1--->1 都是偶數步
 38 */
 39    //然後是對上下左右的搜尋
 40    if(maze[stx][sty+1]!='X')   //向右搜尋
 41    {
 42        maze[stx][sty+1]='X';  //見進入口堵上
 43        dfs(stx,sty+1,dt+1);
 44        maze[stx][sty+1]='.';
 45    }
 46    
 47    if(maze[stx+1][sty]!='X')   //向下搜尋
 48     {
 49         maze[stx+1][sty]='X';  //見進入口堵上
 50        dfs(stx+1,sty,dt+1);
 51         maze[stx+1][sty]='.';
 52     }
 53    if(maze[stx][sty-1]!='X')   //向左搜尋
 54    {
 55         maze[stx][sty-1]='X';  //見進入口堵上
 56        dfs(stx,sty-1,dt+1);
 57         maze[stx][sty-1]='.';
 58    }
 59   if(maze[stx-1][sty]!='X')   //向上搜尋
 60    {
 61         maze[stx-1][sty]='X';  //見進入口堵上
 62         dfs(stx-1,sty,dt+1);
 63         maze[stx-1][sty]='.';
 64    }
 65   return ;
 66 }
 67 
 68 int main()
 69 {
 70     int stx,sty,wall;
 71   while(scanf("%d%d%d",&n,&m,&t),n+m+t)
 72   {
 73       getchar();
 74       wall=0;    //統計障礙物的個數 每次輸入清零
 75    for(int i=1;i<=n;i++)
 76    {
 77        for(int j=1;j<=m;j++)
 78        {
 79            scanf("%c",&maze[i][j]);
 80            if(maze[i][j]=='S')
 81            {
 82                stx=i;     // 標註開始的x軸的位置
 83                sty=j;     // 標註開始的y軸的位置
 84            }
 85            else
 86             if(maze[i][j]=='D')
 87             {
 88                ex=i;    // 標註結束的x軸的位置
 89                ey=j;    // 標註結束的y軸的位置
 90             }
 91             else if(maze[i][j]=='X')
 92             {
 93                wall++;
 94             }
 95        }
 96        getchar();
 97    }
 98     success=false;
 99     maze[stx][sty]='X'; //堵住入口
100     if( n*m-wall<=t )  //因為只有在t時刻door 才打開
101         printf("NOn");
102     else 
103     {
104         dfs(stx,sty,0);
105         if(success)
106             printf("YESn");
107         else
108             printf("NOn");
109     }
110   }
111   return 0;
112 }