hdoj 1010 Tempter of the Bone解题报告

链接:hdoj1010

Tempter of the Bone

**Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 97697 Accepted Submission(s): 26504

**

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
很久之前用java折腾很久过了的题,,,然而C++也折腾了大半天,几个注意点:
1、关于迷宫类型的读入,用字符串数组读入一整行字符串然后进行预处理,预处理很重要,可以简化后面的运算;
for (int i = 0; i < n; i += 1)
        {
            scanf("%s",maze_in);
            for (int j = 0; j < m; j += 1)
            {
                if (maze_in[j]=='X')maze[i][j]=0;
                else
                {
                    maze[i][j]=1;
                    if (maze_in[j]=='S'){
                        maze[i][j]=0;
                        sx=i;sy=j;
                    }
                    if(maze_in[j]=='D'){
                        ex=i;ey=j;
                    }
                }
            }
        }
2、由于深搜的时间复杂度一般都很高,一定要考虑到剪枝该题涉及到的两个方面的剪枝:

a.奇偶剪枝,就是每相邻两步笛卡尔坐标和的奇偶性互异,这应该很显然。
b.曼哈顿距离剪枝,用深度下界判断当前状态是否可行。​

int left=t-k,les=abs(x-ex)+abs(y-ey);
if(les>left)return;
if(left%2!=les%2)return;

其中t为目标时间,k为当前dfs时间;

3、千万要注意出发点的maze在预处理时一定要赋为不可到达;

if (maze_in[j]=='S'){
    maze[i][j]=0;
    sx=i;sy=j;
}

看下面这组数据你就明白了:

2 2 4
S.
.D

好了就这么多,下面是完整AC代码(c++):

#include<iostream>
#include<stdio.h>
#include <math.h>
#include <cmath>

using namespace std;
int n,m,t,sx,sy,ex,ey;
char maze_in[10];
int maze[10][10];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
bool sol=false;
void dfs(int x,int y,int k)
{
//    cout<<x<<","<<y<<" "<<k<<endl;
    if(k>t)return;
    if(k==t&&x==ex&&y==ey)
    {
        sol=true;
        return;
    }
    if(sol)return;
    int left=t-k,les=abs(x-ex)+abs(y-ey);
    if(les>left)return;
    if(left%2!=les%2)return;
    for (int i = 0; i < 4; i += 1)
    {
        int nx=x+dx[i],ny=y+dy[i];
        if (nx>=0&&ny>=0&&nx<=n&&ny<=m&&maze[nx][ny])
        {
            maze[nx][ny]=0;
            dfs(nx,ny,k+1);
            maze[nx][ny]=1;
        }
    }
}
int main()
{
    while (scanf("%d %d %d",&n,&m,&t)&&n)
    {
        sol=false;
        for (int i = 0; i < n; i += 1)
        {
            scanf("%s",maze_in);
            for (int j = 0; j < m; j += 1)
            {
                if (maze_in[j]=='X')maze[i][j]=0;
                else
                {
                    maze[i][j]=1;
                    if (maze_in[j]=='S'){
                        maze[i][j]=0;
                        sx=i;sy=j;
                    }
                    if(maze_in[j]=='D'){
                        ex=i;ey=j;
                    }
                }
            }
        }
        dfs(sx,sy,0);
        if(sol)printf("YES\n");
        else printf("NO\n");    
    }
    return 0;
}

 


文章作者: crazyX
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 crazyX !
评论
 上一篇
图算法 图算法
基本概念图由顶点(vertex,node)和边(edge)组成顶点:图中的数据元素称为顶点.有向图:有方向的图叫有向图.无向图:没有方向的图叫无向图.完全图:有n(n-1)/2条边的无向图称为完全图.有向完全图:具有n(n-1)条弧的有向图
2016-02-17
下一篇 
hdoj 1009 FatMouse' Trade解题报告 hdoj 1009 FatMouse' Trade解题报告
链接:hdoj1009 FatMouse’ Trade**Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total
2016-02-11
  目录