HDU1010 DFS+奇偶剪枝

来源:互联网 发布:第三方物流模式的优化 编辑:程序博客网 时间:2024/05/16 15:10

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 129173    Accepted Submission(s): 34886


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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 

Sample Output
NOYES
 

Author
ZHANG, Zheng
 
Source
ZJCPC2004
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1016 1241 1242 1072 1312 
 

题意:给出一张图,.表示能走的,X表示不能走的,给定步数,问能不能从S走到D(恰好走这么多步),走过的地方不能再走。
解题思路:开始的时候我准备用BFS,毕竟涉及到路径和点的状态的问题。但是很快就意识到不能用BFS,为什么不能用?如果加标记数组,那么就是求最短路,如果不加标记数组,仅仅是不往回走会转圈圈。。。还是用DFS。由于这个图最大才7*7,我就想着直接用DFS暴力解。写了一份交上去,妥妥的TLE,很是不解,毕竟这么小的图都能超,discuss里看了一圈才知道这题是奇偶剪枝。
这个题目的奇偶剪枝其实想起来很简单,无论怎么走,都是直线距离加上一个偶数,否则剪枝。。
AC代码
#include <stdio.h>#include <iostream>using namespace std;#include <stdlib.h>#include <memory.h>#include <math.h>#define maxn 10char pic[maxn][maxn];int flag[maxn][maxn],n,m,num,success,ex,ey;int mm[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};int distance1(int x,int y){    return abs(double(x)-ex)+abs(double(y)-ey);}void dfs(int r,int s,int step){    if(success) return;    if(pic[r][s]=='D'&&step==num)    {        success=1;        return;    }    if(step>=num) return;    int dis=num-step-distance1(r,s);    if(dis<0||dis%2) return;    for(int i=0; i<4; i++)    {        int xx=r+mm[i][0];        int yy=s+mm[i][1];        int tstep=step+1;        if(xx<0||xx>=n||yy<0||yy>=m||flag[xx][yy]||pic[xx][yy]=='X') continue;        flag[xx][yy]=1;        dfs(xx,yy,tstep);        flag[xx][yy]=0;    }}int main(){    //freopen("in.txt","r",stdin);    while(~scanf("%d %d %d\n",&n,&m,&num)&&n+m+num)    {        success=0;        memset(flag,0,sizeof(flag));        for(int i=0; i<n; i++)            gets(pic[i]);        for(int i=0; i<n; i++)            for(int j=0; j<m; j++)                if(pic[i][j]=='D')                {                    ex=i;                    ey=j;                    break;                }        for(int i=0; i<n; i++)            for(int j=0; j<m; j++)                if(pic[i][j]=='S')                {                    flag[i][j]=1;                    dfs(i,j,0);                }        if(success) printf("YES\n");        else printf("NO\n");    }    return 0;}

原创粉丝点击