HDU1010 Tempter of the Bone(回溯 + 剪枝)

题意:

  输入一个 N * M的迷宫,这个迷宫里‘S‘代表小狗的位置,‘X‘代表陷阱,‘D’代表门,‘.’代表可行走的地方,小狗每次可以选择往周围的四个方向行走,问这个小狗能否正好T步找到门。

思路:

  利用回溯 + 剪枝,这道题剪枝特别重要。

剪枝一:

可以把图看成这样:

1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1

则假设从点 a(i + j,横纵坐标之和) 走到点 b(i + j) ,如果 a 和 b 的奇偶性相同,那么从 a 到 b 必须是偶数步.如果 a  和 b 的奇偶性不同则走过的步数必须是奇数步。所以 当 (a + b + T)为奇数时一定不能恰好到达。

剪枝二:

如果已经找到答案就没有要继续求解。

剪枝三:

T大于从S到D的最长路径,小于从S到D的最短路径,则无解。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;

const int MAXN = 11;
char Gra[MAXN][MAXN];
int stepX[] = {0, 0, 1, -1};
int stepY[] = {-1, 1, 0, 0};
int N, M, T;
int cnt;
int beginX, beginY, endX, endY;
int OK;

int check(int x, int y)
{
    if(Gra[x][y] == ‘O‘)  return 0;   // 走到了已经走过的路
    if(Gra[x][y] == ‘*‘)  return 0;   //走出了边界
    if(Gra[x][y] == ‘X‘)  return 0;    //走到了墙
    if(cnt > T)           return 0;  //在此时走的步数已经大于总步数则
                          return 1;
}

void backtrack(int x, int y)
{
    if(x == endX && y == endY ) //到达终点
    {
       if(cnt == T)          //找到了答案
           OK = 1;
    }
    else
    {
        for(int i = 0; i < 4; i++)  //在这点一共有四种选择(状态)
        {
            int tx =  x + stepX[i];
            int ty =  y + stepY[i];
            if(check(tx, ty))    //检查所选择的状态是否合理
            {
                ++cnt;              //记录走过的步数
                Gra[tx][ty] = ‘O‘;   //标记走过的地方
                if(OK) return;    //尤为重要的剪枝 ,如果找到答案,就不用继续递归,
                backtrack(tx, ty);
                Gra[tx][ty] = ‘.‘;  //恢复现场
                --cnt;

            }
        }
    }
}

int main()
{
    //freopen("in.txt","r", stdin);
    while(~scanf("%d%d%d",&N, &M, &T) && N)
    {
        getchar();
        beginX = beginY =  endX = endY = 0;
        memset(Gra, ‘*‘, sizeof(Gra));
        for(int i = 1; i <= N; i++)
        {
            for(int j = 1; j <= M; j++)
            {
                scanf("%c",&Gra[i][j]);
                if(Gra[i][j] == ‘S‘)
                    beginX = i, beginY = j;
                if(Gra[i][j] == ‘D‘)
                    endX = i, endY = j;
            }
            getchar();
        }
        OK = 0;
        if( (T > M * N) || ( T < (abs(beginX - endX) + abs(beginY - endY)) ) || ( (beginX + beginY + endX + endY + T) & 1 ))//剪枝二、三
        {
            printf("NO\n"); continue;
        }
        cnt = 0;
        Gra[beginX][beginY] = ‘O‘;
        backtrack(beginX, beginY);
        if(OK) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
时间: 2024-08-14 03:51:28

HDU1010 Tempter of the Bone(回溯 + 剪枝)的相关文章

hdu1010 Tempter of the Bone(DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 90716    Accepted Submission(s): 24683 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU1010 Tempter of the Bone(DFS+剪枝)

1 # include <stdio.h> 2 #include <cstdio> 3 #include <iostream> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 char map[8][8]; 8 int visit[8][8]; 9 struct node{ 10 int x,y; 11 }; 12 int n,m; 13 node Save,Dog

HDU1010 Tempter of the Bone 【DFS】+【剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70895    Accepted Submission(s): 19535 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU1010 Tempter of the Bone(小狗是否能逃生----DFS)

Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 doggi

HDU 1010 Tempter of the Bone heuristic 剪枝法

本题就是考剪枝法了. 应该说是比较高级的应用了.因为要使用heuristic(经验)剪枝法.要总结出这个经验规律来,不容易.我说这是高级的应用也因为网上太多解题报告都没有分析好这题,给出的程序也很慢,仅仅能过掉,由此看来很多人没有做好这道题. 这里我需要更正一下网上流行的说法:奇偶剪枝法. 其实本题使用奇偶剪枝法并不能太大提高速度,只能说仅仅让使用奇偶剪枝过掉.所以网上说本题使用奇偶剪枝的,其实并不能提高速度. 原因: 奇偶剪枝只能剪枝一次,不能在递归的时候剪枝,因为只要初始化位置符合奇偶性,那

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c

HDU 1010 Tempter of the Bone(DFS剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 129289    Accepted Submission(s): 34906 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU--1010 Tempter of the Bone(深搜+奇偶剪枝)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 我认为的剪枝就是在本来的代码中加入一些附加条件使之不去进行多余的计算,防止超时 奇偶剪枝的知识链接 AC代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int n,m,t,k,flag,starex,starey,endx,endy

HDU1010 --- Tempter of the Bone(dfs+剪枝)

小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里.经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t秒大门会为你敞开”,而他自己所在的棋盘是大小为 N*M 的长方形,他可以向上下左右四个方向移动(不可走有障碍点).棋盘中有一扇门.根据机关的提示,小明顿时明白了,他和朋友必须在第 t 秒到门口.而这一切,没有回头路!因为一旦他移动了,他刚才所在的点就会消失,并且他不能在一个点上停留超过一秒,不然格子