hdu 1010 Tempter of the Bone(搜索+奇偶剪枝)

来源:互联网 发布:c语言编程软件怎么用 编辑:程序博客网 时间:2024/06/13 09:46
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 90187 Accepted Submission(s): 24509
点击打开链接

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
NOYES
题目大意就是找到一条路径从S到D,恰好使得到达所花费的时间等于T。
拿到题目一看就是很清楚的DFS搜路径,然后迅速的敲好,不过T的也很难看。然后搜了搜题解,才知道还有奇偶剪枝这回事,然后看了一下就AC了。不过感觉奇偶剪枝还是没有太学会。
先讲一下奇偶剪枝(虽然我也不太懂,转载别人的)
奇偶剪枝:是数据结构的搜索中,剪枝的一种特殊小技巧。现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,s | | | + — — — e 如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;s — — — — — + | + | + — — — e
如图,为一般情况下非最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);故,若t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;返回,false;反之亦反。
贴上AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#define SIZE 10#define Maxn 1000using namespace std;int n,m,t;char Map[SIZE][SIZE];int vis[SIZE][SIZE];int cnt,flag;int a,b;int hen[] = {-1,1,0,0};int shu[] = {0,0,-1,1};int num,X,Y;void init(){    memset(vis,0,sizeof(vis));    memset(Map,0,sizeof(Map));    num = flag = cnt = 0;}int abs(int a){    return a>0?a:-a;}void DFS(int x,int y){    if(Map[x][y] == 'D'&&cnt == t)        flag = 1;    if(flag)return ;千万别忘了结束,不能让他再继续搜下去,不然剪枝就没啥用了。    if((abs(X-x)+abs(Y-y))%2 != abs(cnt-t)%2)        return ;    for(int i = 0 ; i < 4 ; i ++)    {        if(x+hen[i]<0 || x+hen[i]>=n || y+shu[i]<0 || y+shu[i]>=m)            continue;        if(vis[x+hen[i]][y+shu[i]])            continue;        vis[x][y] = 1;        cnt++;        DFS(x+hen[i],y+shu[i]);        vis[x][y] = 0;        cnt --;    }}void input(){    for(int i = 0 ; i < n ; i ++)    {        scanf("%s",Map[i]);        for(int j = 0 ; j < m ; j ++)        {            if(Map[i][j] == 'S')            {                a = i;b = j;            }            else if(Map[i][j] == 'X')            {                 vis[i][j] = 1;            }            else if(Map[i][j] == 'D')            {                X = i;                Y = j;                num ++;            }            else num ++;        }    }}int main(){    freopen("in.txt","r",stdin);    while(scanf("%d%d%d",&n,&m,&t)!=EOF)    {        if(n==0 &&m==0 && t==0)            break;        init();        input();        vis[a][b] = 1;        if(num >= t)            DFS(a,b);        if(flag)            printf("YES\n");        else            printf("NO\n");    }    return 0;}



0 0