标签:
题目:
Tempter of the Bone |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 1134 Accepted Submission(s): 379 |
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 |
Recommend JGShining |
题目分析:
“判断是否能够从起点达到终点”,这种题用BFS就能轻松搞定。然后这道题的特点就在于“在指定时间的约束下”。那么这时候我们可以使用DFS来做。 本来想自己写一下想法的。但是发现网上有人写的比我将要写的详细多了。所以投一下懒,用一下别人的总结。
代码如下:
/* * a.cpp * * Created on: 2015年2月24日 * Author: Administrator */ #include <iostream> #include <cstdio> using namespace std; const int maxn = 9; char map[maxn][maxn];//地图矩阵.用于存储地图的情况 int n;//行数 int m;//列数 int t;//目标时间 int x1,y1;//起点 int x2,y2;//终点 int dir[4][2]={//方向矩阵 {1,0}, {-1,0}, {0,1}, {0,-1} }; /** * 判断下一步是否合法 */ bool check(int x,int y){ if( x < 0 || x >= n || y <0 || y >= m){//如果坐标越界 return false;//则表明下一步不合法 } return true;//否则下一步合法 } /** * DFS。 * si:当前结点的行数 * sj:当前节点的列数 * cnt:到达(si,sj)是所用的时间 */ bool dfs(int si,int sj,int cnt){ if(si == x2 && sj == y2 && cnt == t){//如果在指定时间内到达目标点 return true;//返回true } /** * 如果当前结点越界。这个判断是不需要的。 * 加了还可能超时。因为是否越界的判断在产生下一节点的时候就已经做了 */ // if(check(si,sj) == false){ // return false; // } /** * * t-cnt:表示剩余时间 * abs(x2-si) + abs(y2-sj):从当前节点道目标节点所需要的时间 */ int temp = (t - cnt) - (abs(x2-si) + abs(y2-sj)); //最短路径剪枝 if(temp < 0){//如果剩余时间不足以到达目标节点 return false;//返回false,表示这条路径不成功 } //奇偶剪枝 if(temp&1){ return false; } int i; for(i = 0 ; i < 4 ; ++i){//遍历当前节点的相邻结点 int tempi = si+dir[i][0];//计算下一节点的坐标 int tempj = sj+dir[i][1]; if(check(tempi,tempj) == false){//如果计算出来的下一届点的坐标不合法 continue;//则跳过这一结点,计算下一结点 } if(map[tempi][tempj] != ‘X‘){//如果当前节点不是墙壁 map[tempi][tempj] = ‘X‘;//江当前节点设置为墙壁 bool flag = dfs(tempi,tempj,cnt+1);//沿着这一节点往下搜 if(flag == true){//如果这一条路径可行 return true;//则返回true.表示目标节点可以到达 } //当知心以下代码的时候表示通过这一节点无法到达目标节点 map[tempi][tempj] = ‘.‘;//拿奖当前节点重新设置成‘.‘可用状态 } } return false;//如果经过上面都无法找到一条路径.那么到现在的时候就已经表明目标节点无法到达 } int main(){ while(scanf("%d%d%d",&n,&m,&t) != EOF,n){ int i; int j; for(i = 0 ; i < n ; ++i){ cin >> map[i]; } int wall = 0;//用于包村墙壁的数量 for(i = 0 ; i < n ; ++i){ for(j = 0 ; j < m ; ++j){ if(map[i][j] == ‘S‘){//记录起点的位置 x1 = i; y1 = j; }else if(map[i][j] == ‘D‘){//记录终点的位置 x2 = i; y2 = j; }else if(map[i][j] == ‘X‘){//统计墙壁的数量 wall++; } } } //路径剪枝 int nums = n*m - wall;//计算可走的步数 if(nums < t){//如果可走的步数<指定的时间 printf("NO\n");//那么表明路已经走完了也还没有达到指定时间.name返回false.表示没有这样的一条路径 continue; } map[x1][y1] = ‘X‘;//奖起点设置为墙壁‘x‘ bool result = dfs(x1,y1,0);//从起点开始遍历 if(result == true){//如果resultweighttrue,表明有这么一条路径 printf("YES\n"); }else{ printf("NO\n");//否则表明没有这样一条路径 } } return 0; }
(hdu step 4.3.1)Tempter of the Bone(在特定的时间约束下,判断是否能够从起点达到终点)
标签:
原文地址:http://blog.csdn.net/hjd_love_zzt/article/details/43924951