hdu 1010

来源:互联网 发布:软件开发工程师是什么 编辑:程序博客网 时间:2024/06/06 07:20
百度一下奇偶剪枝,看一下这个博客应该没问题了点击打开链接
#include<cstdio>#include<iostream>#include<vector>#include<queue>#include<cstring>#include<cmath>using namespace std;const int maxn = 7 + 1;char G[maxn][maxn];int n, m, t, ex, ey;struct node{    int x, y, step;}beg;int vis[maxn][maxn];const int dir[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};inline bool check(node q){    if(q.x < n && q.x >= 0 && q.y < m && q.y >= 0 && !vis[q.x][q.y] && G[q.x][q.y] != 'X') return true;    return false;}int dfs(node q){    int temp = t - q.step - abs(q.x - ex) - abs(q.y - ey);    if(temp < 0 || temp & 1) return false;    if(G[q.x][q.y] == 'D')    {        //printf("%d %d %d\n", q.x, q.y, q.step);        if(q.step == t) return true;        else return false;    }    for(int i = 0; i < 4; i++)    {        node sq;        sq.x = q.x + dir[i][0];        sq.y = q.y + dir[i][1];        sq.step = q.step;        sq.step++;        if(check(sq))        {            //printf("%d %d\n", sq.x, sq.y);            vis[sq.x][sq.y] = 1;            if(dfs(sq)) return true;            vis[sq.x][sq.y] = 0;        }    }    return false;}int main(){    while(scanf("%d%d%d", &n, &m, &t) == 3 && n)    {        for(int i = 0; i < n; i++)            for(int j = 0; j < m; j++)            {                cin>>G[i][j];                if(G[i][j] == 'S')                    beg = (node){i, j, 0};                if(G[i][j] == 'D')                {                    ex = i;                    ey = j;                }            }        memset(vis, 0, sizeof(vis));        vis[beg.x][beg.y] = 1;        if(dfs(beg)) printf("YES\n");        else printf("NO\n");    }    return 0;}