HDU 1010 Tempter of the Bone

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1010

题目类型 DFS

知识点 奇偶剪枝

不知道的同学可以了解下

http://baike.baidu.com/view/7789287.htm?fr=aladdin

思路 深搜标记每个走过的点, 然后再回溯!将每一条路径都搜出来

 

#include <iostream>

#include <queue>

#include <vector>

#include <cstdio>

#include <cstring>

#include <cmath>

using namespace std;

#define maxn 50

int m, n, step;

struct Point

{

    int x, y;

}Ps, Pe;

char maps[maxn][maxn];

bool vis[maxn][maxn];

int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}, OK;

void DFS(int x,int y,int k);

int main()

{

    while(scanf("%d%d%d",&m,&n,&step), m+n+step)

    {

        OK = 0;

        memset(vis,0,sizeof(vis));

        for(int i=0 ; i<m; i++)

        {

            scanf("%s",maps[i]);

            for(int j=0; j<n; j++)

            {

                if(maps[i][j] == 'S')

                    Ps.x = i, Ps.y = j;

                if(maps[i][j] == 'D')

                    Pe.x = i, Pe.y = j;

            }

        }

        int s = (Ps.x + Ps.y)%2 , e = (Pe.x + Pe.y)%2;

        if( (s+e)%2 == step%2 )

            DFS(Ps.x,Ps.y,step);



        if(OK)

            printf("YES\n");

        else

            printf("NO\n");

    }

    return 0;

}

void DFS(int x,int y,int k)

{

    if(OK)

        return ;

    if(x == Pe.x && x == Pe.y && k == 0)

    {

        OK = 1;

        return ;

    }

    int len = abs(Pe.x - x) + abs(Pe.y - y);



    if(len < step)

        return ;



    for(int i=0; i<4; i++)

    {

        int nx = x + dir[i][0];

        int ny = y + dir[i][1];

        if(nx >= 0 && nx < m && ny >=0 && ny < n && maps[nx][ny] != 'X' && !vis[nx][ny])

        {

            vis[nx][ny] = 1;

            DFS(nx,ny,k-1);

            vis[nx][ny] = 0;

        }

    }

}

 

你可能感兴趣的:(HDU)