码迷,mamicode.com
首页 > 其他好文 > 详细

【深搜加剪枝五】HDU 1010 Tempter of the Bone

时间:2014-05-01 19:03:37      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   code   java   tar   ext   width   color   get   int   

Tempter of the Bone

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


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
 

Author
ZHANG, Zheng
 

Source
 

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

 

首先,题意方面:
要恰好T时间到才可以,不能小于T时间
其次,注意边界问题,是>还是>=  !!!
最后就是剪枝了
这里大致剪了三步:见注释
其中第二步---奇偶剪枝

神马是奇偶剪枝,以一个4*4的图举例,当前坐标为偶数和为0,否则为1

0 1 0 1

1 0 1 0

0 1 0 1

1 0 1 0

从‘0’到‘0’,或从‘1’到‘1’,必须经过偶数步,
而从‘0’到‘1’或从‘1’到’0‘,需要奇数步。

也就是说,如果当前的狗所在的坐标与终点的坐标奇偶性不一样,那么狗需要走奇数步。
同理,如果狗所在坐标与终点的坐标奇偶性一样,那么狗需要走偶数步数。
判断剩余步数  和   还需要走的步数之间的奇偶性 不同直接返回。

第三步估价剪枝也要注意。

#include<stdio.h>
#include<memory.h>
#include <iostream>
#include<cmath>
using namespace std;
#define size 10
int N,M,T;
char maze[size][size];
int startx,starty,endx,endy;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool flag;
void dfs(int x, int y, int time)
{
    if (x<=0 || x>N || y<= 0 || y>M) return;
    if (flag) return;  //1.找到解后还有部分仍在搜索,这个为了让没必要的搜索终止
    if (x == endx && y == endy && time == T)
    {
        flag = true;
        return;
    }
    int temp = (T - time) - abs(x - endx) - abs(y - endy);//2.奇偶性剪枝
    if(temp<0 || temp & 1)  return;
    for (int i = 0; i<4; i++)
    {
        int nextx=x+dx[i];
        int nexty=y+dy[i];
        if (maze[nextx][nexty] != ‘X‘)
        {
            maze[nextx][nexty] = ‘X‘;
            dfs(nextx, nexty, time + 1);
            maze[nextx][nexty] = ‘.‘;//回溯
        }
    }
    return;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(~scanf("%d%d%d",&N,&M,&T)){
            int num=0;
            if(N==0&&M==0&&T==0){
                break;
            }
            for(int i=1;i<=N;i++){
                for(int j=1;j<=M;j++){
                    cin>>maze[i][j];
                    if(maze[i][j]==‘S‘){
                        startx=i;
                        starty=j;
                    }
                    if(maze[i][j]==‘D‘){
                        endx=i;
                        endy=j;
                    }
                    if(maze[i][j]==‘X‘){
                        num++;
                    }
                }
            }
            flag=false;
            maze[startx][starty]=‘X‘;//这里注意
            if(N*M-num <= T){   //如果可以走的步数少于时间直接NO!所谓估价剪枝
                printf("NO\n");
                continue;
            }
            dfs(startx,starty,0);
            if(!flag)
                printf("NO\n");
            else{
                printf("YES\n");
            }

    }
    //fclose(stdout);
    //fclose(stdin);
    return 0;
}







【深搜加剪枝五】HDU 1010 Tempter of the Bone,码迷,mamicode.com

【深搜加剪枝五】HDU 1010 Tempter of the Bone

标签:des   style   class   code   java   tar   ext   width   color   get   int   

原文地址:http://www.cnblogs.com/zhaokongnuan/p/45a6e0ec95e19fce5b2a4fed00a88193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!