hdu1010 奇偶剪枝

//abs(x-ex)+abs(y-ey)表示现在所在的格子到目标格子的距离(不能走对角线)

//t-cnt是实际还需要的步数,将它们做差

//如果temp<0 或者 temp为奇数,那就不可能到达

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string>
#include<algorithm>
#include<sstream>
#include<set>
#include<map>
#include<cctype>
#include<vector>
#include<math.h>
using namespace std;
int n, m,t;
int wall;
char a[10][10];
int d[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
bool flag;
int dx, dy, s1, s2;
void dfs(int x, int y, int t1)
{
    if (x < 0 || y >= m || x >= n || y < 0 )
        return;
    if (x == s1 && y == s2 && t == t1)
    {
        flag = 1;
        return;
    }
    int temp;
     temp = (t - t1) - abs(x - s1) - abs(y - s2);
     if (temp < 0 || temp % 2)
         return;
    for (int i = 0; i < 4; i++)
    {
        int nx = x + d[i][0];
        int ny = y + d[i][1];
        if (a[nx][ny] != 'X')
        {
            a[nx][ny] = 'X';
            dfs(nx, ny, t1+1);
            if (flag)
                return;
            a[nx][ny] = '.';
        }
    }
    return;
}
        
int main()
{
    while (scanf("%d%d%d", &n, &m, &t) != EOF)
    {
        getchar();
        if (n == 0 && m == 0 && t == 0)
            break;
        for (int i = 0; i < n; i++)
            scanf("%s", a[i]);
        flag = 0;
        wall = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (a[i][j] == 'S')
                {
                    dx = i;
                    dy = j;
                }
                else if (a[i][j] == 'D')
                {
                    s1 = i;
                    s2 = j;
                }
                else if (a[i][j] == 'X')
                    wall++;
            }
        }        
        if (n*m - wall < t)
        {
            printf("NO\n");
            continue;
        }
        a[dx][dy] = 'X';
        dfs(dx, dy, 0);
        if (flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

 

posted @ 2016-10-04 13:56  web之路  阅读(76)  评论(0)    收藏  举报