• 1010 Tempter of the Bone (杭电) (图Graph)


    Tempter of the Bone

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

    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
    --------------------------------------------------------------------------------------------------------------------------------------
    此题是图的遍历问题,注意读题:门在第t秒会短暂地开一下。需要找到S到D是否有距离为t的路径。不是最短路径,所以不能选择BFS算法。使用DFS算法,涉及递归,所以需要想办法去尽可能prune掉不必要的遍历。
     1 #include <cstring>
     2 #include <cmath>
     3 #include <iostream>
     4 
     5 using namespace std;
     6 const int maxn=10;
     7 char maze[maxn][maxn]; //迷宫地图
     8 bool visited[maxn][maxn]; //访问过的矩阵
     9 int direction[4][2]={{-1,0},{0,-1},{0,1},{1,0}}; //可以跳跃的方向
    10 int n,m,t;
    11 int start_x,start_y,end_x,end_y;
    12 bool DFS(int x,int y,int step){//经过step步访问到坐标(x,y),进行深度优先搜索
    13     if(maze[x][y]=='D'&&step==t)
    14         return true;
    15     if(step>t)                                     //重要的是在DFS中进行prune,剪枝提高效率
    16         return false;
    17     if(step+abs(end_x-x)+abs(end_y-y)>t)        //如果(x,y)到(end_x,end_y)的理论最小值+step>t,剪掉
    18         return false;
    19     if((t-step-abs(end_x-x)-abs(end_y-y))&0x1) //如果t-step与理论最小值的差值应为偶数,若奇数则剪掉
    20         return false;
    21 
    22     visited[x][y]=true;
    23     for(int i=0;i<4;i++){
    24         int tmp_x=x+direction[i][0],tmp_y=y+direction[i][1];
    25         if(maze[tmp_x][tmp_y]!='X'&&!visited[tmp_x][tmp_y])
    26             if(DFS(tmp_x,tmp_y,step+1))return true;
    27     }
    28     visited[x][y]=false;
    29     return false;
    30 }
    31 int main(){
    32     while(cin>>n>>m>>t&&n!=0){
    33         memset(maze,'X',sizeof(maze));
    34         memset(visited,false,sizeof(visited));
    35         for(int i=1;i<=n;i++)
    36             for(int j=1;j<=m;j++){ //在maze的外围加上了一圈'X'的围栏
    37                 cin>>maze[i][j];
    38                 switch(maze[i][j]){
    39                 case 'S':
    40                     start_x=i,start_y=j;
    41                     break;
    42                 case 'D':
    43                     end_x=i,end_y=j;
    44                     break;
    45                 }
    46             }
    47 
    48         cout<<(DFS(start_x,start_y,0)?"YES":"NO")<<endl;
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    火车进出站(POJ1363)
    字符串反转,栈模拟(ZOJ1151)
    模拟网页的浏览Stack(POJ1028)
    Codeforces Round #347 (Div.2)_B. Rebus
    Codeforces Round #347 (Div.2)_A. Complicated GCD
    BFS模板
    广搜破解密码(HDU1195)
    DFS+BFS(POJ3083)
    砍树,POJ(2665)
    快速幂取模,POJ(1995)
  • 原文地址:https://www.cnblogs.com/purgiant/p/3202542.html
Copyright © 2020-2023  润新知