HDU 1010(dfs+奇偶剪枝)

题意:

'X': a block of wall, which the doggie cannot enter; 
'S': the start point of the doggie; 
'D': the Door; or
'.': an empty block.

doggie在起点'S'的时候地板开始下陷,他必须马上移动,'X'是墙,不能通过,'.'是地板,可以通过,但是一走过地板就开始下陷,必须刚好在T时间到达'D'大门,再能逃生。

 

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
using namespace std;

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

void wall(string* maze, int N, int M)
{
    maze[N+1] = maze[0] = string(M+2, 'X');
    for (int i = 1; i <= N; ++i)
        maze[i] = 'X' + maze[i] + 'X';
}

void getXY(string* maze, int N, int M, int& x, int& y, char c)
{
    for (int i = 1; i <= N; ++i)
        for (int j = 1; j <= M; ++j)
        {
            if (maze[i][j] == c)
            {
                x = i;
                y = j;
                return;
            }
        }
}

bool dfs(string* maze, bool(*visited)[9], int x, int y, int T)
{
    if (!T)
    {
        if (maze[x][y] == 'D')
            return true;
        return false;
    }
    visited[x][y] = true;
    for (int i = 0; i < 4; ++i)
    {
        int X = x + direction[i][0], Y = y + direction[i][1];
        if (visited[X][Y] == false && maze[X][Y] != 'X')
            if (dfs(maze, visited, X, Y, T-1))
                return true;
    }
    visited[x][y] = false;
    return false;
}

bool result(string* maze, int N, int M, int T)
{
    wall(maze, N, M);
    int x, y, ex, ey;
    getXY(maze, N, M, x, y, 'S');
    getXY(maze, N, M, ex, ey, 'D');
    int min_step = abs(ex-x) + abs(ey-y);
    if (T < min_step || ((T - min_step) & 1))
        return false;
    bool visited[9][9];
    memset(visited, false, sizeof(visited));
    return dfs(maze, visited, x, y, T);
}

int main()
{
    int N, M, T;
    int i;
    string maze[9];
    while (cin >> N >> M >> T, N || M || T)
    {
        while (getchar() != '\n');
        for (i = 1; i <= N; ++i)
            getline(cin, maze[i]);
        cout << (result(maze, N, M, T) ? "YES" : "NO") << endl;
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值