MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
题目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=1010
题目描述:
代码
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16817 Accepted Submission(s): 4693
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
题目分析 :
传说中的 搜索题中具有里程碑意义的题目........
很好,很强大. 做过过这题大概也就明白了 搜索中的 奇偶剪枝以及路径. 加了剪枝的直接结果就是 TLE 和 46MS AC.......
刚开始做的时候,也没明白剪枝到底有什么作用, 所以直接敲了个DFS代码就交了, 答案很明显....TLE. 然后就去学习PPT去了.
所谓奇偶剪枝 :
- 把矩阵标记成如下形式:
- 0,1,0,1,0
- 1,0,1,0,1
- 0,1,0,1,0
- 1,0,1,0,1
- 很明显,如果起点在0 而终点在1 那显然 要经过奇数步才能从起点走到终点,依次类推,奇偶相同的偶数步,奇偶不同的奇数步
- 在读入数据的时候就可以判断,并且做剪枝,当然做的时候并不要求把整个矩阵0,1刷一遍,读入的时候起点记为(Si,Sj) 终点记为(Di,Dj) 判断(Si+Sj) 和 (Di+Dj) 的奇偶性就可以了
所谓路径剪枝:
矩阵的大小是N*M 墙的数量记为wall 如果能走的路的数量 N*M - wall 小于时间T,就是说走完也不能到总的时间的,这显然是错误的,可以直接跳出了.
另外还有就是, 当DFS深度 > T 时,显然也不用继续找下去了. 那狗已经挂了.
所以在经过这3次剪枝后, 时间就大大缩短了.
值得一提的是!!! 这题我WA了很久, 一直找不原因, 因为数据不一定是按严格矩阵排列的!! 也可能都在一行!!!! 所以 无论是 gets 还是 getchar 都错的很冤枉.
使用 cin 和 scanf ("%s") 后 AC 了, 在此感谢 AMB神牛的帮忙.......
代码如下:
/*
MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
Author By : MiYu
Test : 1
Program : HDU1010
*/
#include <iostream>
#include <ctime>
using namespace std;
typedef struct pos{
int x,y;
void setPos( int a = 0,int b = 0 ){ x = a; y = b; }
}POS;
POS start,end;
const int START = 10; //看了就知道啥意思
const int DOOR = 20;
const int WALL = 0;
const int ROAR = 1;
int N = 0,M = 0,T = 0; //输入的
int maze[11][11]; //很明显是迷宫地图
int d[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
int f = 1,roarCount = 0; //标记是否找到路 , 记录可以走的路的个数
int DFS ( int x,int y,int n )
{
if ( n == T ) //时间用完了, 走到出口没 ?
{
if ( x == end.x && y == end.y )
f = 0;
return 0;
}
if ( f == 0 ) //已经找到路了, 不用找了
return 0;
int t = T - n - abs( x-end.x ) - abs( y-end.y );
if ( t < 0 || t % 2 == 1 ) //不够时间了 或 不可能走到(奇偶剪枝)
return 0;
for ( int i = 0; i != 4; ++ i )
{
if ( maze[ x+d[i][0] ][ y+d[i][1] ] != WALL ) //先看看下一步能不能走
{
maze[x+d[i][0]][y+d[i][1]] = WALL; //走过后就不能走了
DFS ( x+d[i][0], y+d[i][1], n + 1 ); //走到下一个位置
if ( f == 0 ) //已经找到路了, 不用找了
return 0;
maze[x+d[i][0]][y+d[i][1]] = ROAR; //没找到路,回溯下
}
}
return 0;
}
void init() //在主函数一堆, 难看, 放外面了, 不解释
{
memset ( maze, 0, sizeof ( maze ) );
f = 1, roarCount = 0;
for ( int i = 1; i <= N; ++ i )
{
char ch;
for ( int j = 1; j <= M ; ++ j )
{
cin >> ch;
switch ( ch )
{
case 'S': maze[i][j] = START; start.setPos ( i,j ); break;
case '.': maze[i][j] = ROAR; roarCount ++; break;
case 'X': maze[i][j] = WALL; break;
case 'D': maze[i][j] = DOOR; end.setPos ( i,j ); roarCount ++; break;
}
}
}
}
int main ()
{
while ( cin >> N >> M >> T, N + M + T )
{
init ();
if ( roarCount >= T ) //当然要保证能走的路比开门的时间要多
{
maze[start.x][start.y] = WALL;
DFS( start.x, start.y, 0 );
}
puts ( f == 1 ? "NO" : "YES" );
}
return 0;
}
另外附上一网友详细解释: