DFS剪枝+奇偶剪枝

1、介绍
在搜索算法中剪枝是算法优化策略,通过剪去一些不必要的枝条减少遍历过程。
2、分类
剪枝大体种剪枝方式:可行性剪枝、最优性剪枝。
2.1 可行性剪枝
这种方法判断后续搜索能否得到答案,如果不能提前回溯
2.2 最优性剪枝
最优性剪枝,又称为上下界剪枝。记录当前得到的最优值,如果当前结点已经无法产生比当前最优解更优的解时,可以提前回溯。

奇偶剪枝
部分内容来自https://blog.csdn.net/chyshnu/article/details/6171758
把矩阵看成如下形式:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
从为 0 的格子走一步,必然走向为 1 的格子 。
从为 1 的格子走一步,必然走向为 0 的格子 。
即:
从 0 走向 1 必然是奇数步,从 0 走向 0 必然是偶数步。

所以当遇到从 0 走向 0 但是要求时间是奇数的或者 从 1 走向 0 但是要求时间是偶数的,都可以直接判断不可达!

比如一张地图

S...  
....  
....  
....  
...D 

要求从S点到达D点,此时,从S到D的最短距离为s = abs ( dx - sx ) + abs ( dy - sy )。

如果地图中出现了不能经过的障碍物:

S..X  
XX.X  
...X  
.XXX  
...D 

此时的最短距离s’ = s + 4,为了绕开障碍,不管偏移几个点,偏移的距离都是最短距离s加上一个偶数距离。

就如同上面说的矩阵,要求你从0走到0,无论你怎么绕,永远都是最短距离(偶数步)加上某个偶数步;要求你从1走到0,永远只能是最短距离(奇数步)加上某个偶数步。

题目链接:原题链接

Tempter of the Bone

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

参考代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<stack>
#include<queue>
#include<list>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f//0x7fffffff
#define PI acos(-1)
#define ll long long
int dir[8][2] = {{0,1}, {0,-1}, {-1,0}, {1,0}, {-1,1}, {1,-1}, {1,1}, {-1,-1}};

int n, m, t;
int startx, starty, endx, endy;
int mark[10][10];
char Map[10][10];
bool flag;
void dfs(int nowx, int nowy, int t){
    if(t<0||(0==t&&(nowx != endx||nowy != endy)))
        return;
    if(t==0&&nowx == endx&&nowy == endy){
        flag = true;
        return;
    }
    for(int i=0;i<4;++i){
        int nextx = nowx+dir[i][0];
        int nexty = nowy+dir[i][1];
        if(nextx>=0&&nextx<n&&nexty>=0&&nexty<m&&mark[nextx][nexty]==0){
            mark[nextx][nexty] = 1;
            dfs(nextx, nexty, t-1);
            mark[nextx][nexty] = 0;
        }
    }
}
int main(){
    while(scanf("%d %d %d", &n, &m, &t)!=EOF&&(n||m||t)){
        memset(mark, 0, sizeof(mark));
        //printf("%d\n", mark[3][3]);
        for(int i=0;i<n;++i)
        for(int j=0;j<m;++j){
            cin >> Map[i][j];
            if(Map[i][j]=='S')
                startx = i, starty = j, mark[i][j]=1;
            else if(Map[i][j]=='D')
                endx = i, endy = j;
            else if(Map[i][j]=='X')
                mark[i][j] = 1;
        }
        int Min = abs(endx-startx)+abs(endy-starty);//最短路径
        if((t-Min)%2==1||t<Min){//奇偶性判断
            printf("NO\n");
            continue;
        }
        flag = false;
        dfs(startx, starty, t);
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值