这题是一场经典的剪枝,因为DFS太浪费时间了,他的时间复杂度是O(n2),所以必须要优化,这里用到了奇偶剪枝,学长那里get到的
题目的链接如下:
http://acm.hdu.edu.cn/showproblem.php?pid=1010
C - 1010
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
这题的大意就是狗狗站在S处,然后他每一秒能走一个位置,然后X为墙,穿不过去,其余的点能走,而且每走一个’ . ‘这块砖就会沉下去,就是说一块砖只能走一次,他要在门开启的时间刚好到达D点它才能走出去(因为门只开一秒),能走出去就输出“YES”否则就输出“NO”。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std ;
int sx, sy;
char maze[100][100];//迷宫的大小
int vis[101][101];//访问很方便
int n, m, t; //迷宫的长和宽和出口出现的时间t
int flag, k; // 开始的时间和走到的时间一致就是1 然后k是总点数
int ex, ey; //出口的坐标
int dirt[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void dfs(int x, int y, int tt)
{
if(x == ex && y == ey && tt == t){
flag = 1 ;
return ;
}
if(tt == t) return ;
if(abs(ex - x) + abs(ey - y) > (t - tt)) return ;
//vis[x][y] = '1';
for(int i = 0 ; i < 4 ; i++){
int tx = x + dirt[i][0];
int ty = y + dirt[i][1];
if(tx < 0 || tx >= n || ty < 0 || ty >= m || maze[tx][ty] == 'X') continue;
//dfs(tx, ty, tt+1);
if(vis[tx][ty] == 0){
vis[x][y] = 1;
dfs(tx, ty, tt+1);/*这个地方由递归可得,不用让tt再-- 因为这个tt是局部变量
,递归有几层就会生成几个tt,但是每层的tt值不相同*/
if(flag) return ;
vis[x][y] = 0 ;
}
}
}
int main()
{
while(cin >> n >> m >> t && (n||m||t)){
memset(vis, 0, sizeof(vis));
k = 0 ;
flag = 0 ;
for(int i = 0 ; i < n ; i++)
cin >> maze[i];
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
//scanf("%s", &maze[i][j]);
if(maze[i][j] == 'S')
sx = i , sy = j;
if(maze[i][j] == 'D')
ex = i , ey = j;
if(maze[i][j] == '.')
k++;
}
}
if((abs(sx-ex) + abs(sy-ey) + t) & 1 || k+1 < t || (abs(sx-ex) + abs(sy-ey) > t)){/*这里就是奇偶枝
(奇数加偶数还是奇数,第一个判别式就是说两个之间的相对距离和t不同时是奇数或者偶数),
然后第二个就是最多的步数加一还不够t,就结束,第三个就是两个的相对距离都大于t(根号
下x²+y²是相对距离 相对距离一定小于等于他们之间的距离。)那么肯定不满足条件,直接输
出NO即可。*/
printf("NO\n");
continue;
}
vis[sx][sy] = 1;
dfs(sx, sy, 0);
if(flag)
cout << "YES" << endl;
else cout << "NO" << endl;
}
// system("pause");
return 0 ;
}