Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24023 Accepted Submission(s): 6616
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
这题与普通的走迷宫有些不相同之处:1.规定那个时间到达(不是规定时间内到达);2.走过的路不能再走。第一次做超时了,要剪枝,后来想到有一个重要剪枝---奇偶剪枝!写了就AC了。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
bool step[8][8];
char ch[15][15];
int n, m, s, flag;
void init()
{
flag = 0;
memset(step, false, sizeof(step));
}
void dfs(int x, int y, int t)
{
if(flag) return; //若已经找到,返回
if(x<0 || y<0 || x>=n || y>=m) return; //若越界,返回
if(t > s) return; //若时间已经超过出现时间,返回
if(ch[x][y] == 'X') return; //若是墙,返回
if(ch[x][y] == 'D')
{
if(t==s) //若时间刚刚好
{
flag = 1;
return;
}
//重要剪枝:去到'D'点是偶数(奇数)步,那它出来的时间一定要偶数(奇数)才能去到
if(t%2 != s%2)
{
flag = 2;
return;
}
}
if(step[x][y]) return; //若该点已经走过,则返回
step[x][y] = true; //否则,更新该点为走过,并开始搜索
//往4个方向进行dfs深搜
dfs(x+1, y, t+1);
dfs(x-1, y, t+1);
dfs(x, y+1, t+1);
dfs(x, y-1, t+1);
step[x][y] = false; //若该点是走不通的,更新该点为未走过,往上一步退
}
int main()
{
int i, j;
while(scanf("%d %d %d", &n, &m, &s) != EOF)
{
if(!m && !n && !s) break;
init();
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
cin>>ch[i][j];
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
if(ch[i][j] == 'S')
dfs(i, j, 0); //找到入口,开始搜索
if(flag == 1) printf("YES\n");
else printf("NO\n");
}
return 0;
}
分享到:
相关推荐
HDU 1010-2500解题报告,ACMer可以借鉴一下
杭州电子科技大学oj平台上的第1010题,是关于搜索的题目,很不错的题
ACM比赛解题报告,包括hdu1880、zoj1010、zoj1015,为原创的报告,算法不一定最优的
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases. In each test case, there are two integers k,m(1≤k≤1018,1≤m≤100). Output For each test case, print ...
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
hdu动态规划算法集锦