码迷,mamicode.com
首页 > 其他好文 > 详细

题解报告:hdu 1010 Tempter of the Bone

时间:2018-05-12 17:30:02      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:lock   blocks   real   out   oid   second   ret   please   时间   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

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
解题思路:注解在代码里。这里直接贴一篇大牛的讲解吧!很详细,也很容易懂。链接:hdu1010详细题解
AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 //迷宫地图
 4 //X: 墙壁,小狗不能进入
 5 //S: 小狗所处的起始位置
 6 //D: 迷宫的门
 7 //. : 空的方格,表示可以经过的点
 8 char maps[8][8];
 9 int n,m,t,di,dj;//n行,m列,t是规定时间内到达,(di,dj):门的位置
10 bool escape;//表示是否逃脱
11 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上、下、左、右(方向数组)
12 void dfs(int si,int sj,int cnt)//表示起始位置为(si,sj),要求在第cnt秒到达门的位置
13 {
14     if(si>n || sj>m || si<=0 || sj<=0)return;//处理越界情况,直接退出
15     if(si==di && sj==dj && cnt==t){escape=true;return;}//到达出口
16     int tmp=(t-cnt)-abs(si-di)-abs(sj-dj);//abs(x-ex)+abs(y-ey)表示现在所在的格子到目标格子的距离(不能走对角线)剪枝的核心代码
17     if(tmp<0 || tmp&1)return;//t-cnt是实际还需要的步数,将他们做差,如果tmp<0或者tmp为奇数,那就不可能到达!
18     for(int i=0;i<4;i++){//深搜当前方向的每个方向
19         if(maps[si+dir[i][0]][sj+dir[i][1]]!=X){
20             maps[si+dir[i][0]][sj+dir[i][1]]=X;//标记为墙壁,表示不能再走过
21             dfs(si+dir[i][0],sj+dir[i][1],cnt+1);//深搜
22             if(escape)return;//若找到,直接返回
23             maps[si+dir[i][0]][sj+dir[i][1]]=.;//同时还原本来不是墙但被标记的墙(回溯)
24         }
25     }
26     return;
27 }
28 int main()
29 {
30     int si,sj;//表示起点的坐标
31     while(cin>>n>>m>>t && (m+n+t)){
32         int wall=0;
33         for(int i=1;i<=n;i++){//从1开始,因为在遍历该点的四个方向时才不会越界的危险
34             for(int j=1;j<=m;j++){
35                 cin>>maps[i][j];
36                 if(maps[i][j]==S){si=i;sj=j;}//标记小狗的位置
37                 else if(maps[i][j]==D){di=i;dj=j;}//标记出口的位置
38                 else if(maps[i][j]==X)wall++;//计算墙的数量
39             }
40         }//如果剩下的路径长度小于所需t步,注意n*m-wall==t表示地图上可以走的路径长度比t少1,因为此时只有n*m-wall-1条边
41         if(n*m-wall<=t){cout<<"NO"<<endl;continue;}
42         escape=false;//标记为false,表示还没找到
43         maps[si][sj]=X;//直接标记出发点为墙,表示不能返回
44         dfs(si,sj,0);//从起始位置深搜
45         if(escape)cout<<"YES"<<endl;//逃脱成功
46         else cout<<"NO"<<endl;
47     }
48     return 0;
49 }

 

题解报告:hdu 1010 Tempter of the Bone

标签:lock   blocks   real   out   oid   second   ret   please   时间   

原文地址:https://www.cnblogs.com/acgoto/p/9029151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!