hdu 1010 Tempter of the Bone

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
一开始用bfs写的wa掉了,点开discus然后用dfs写tle %>_<%。。查了下资料发现有奇偶剪枝这东西,敲上ok。。
奇偶剪枝参见度娘: http://baike.baidu.com/history/%E5%A5%87%E5%81%B6%E5%89%AA%E6%9E%9D/76619083

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::abs;
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::map;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 10;
typedef unsigned long long ull;
bool vis[N][N];
char G[N][N];
int H, W, T, Sx, Sy, Dx, Dy;
const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };
bool dfs(int x, int y, int s) {
    // 找到一个解就直接返回
    if (s == T && x == Dx && y == Dy) return true;
    int tmp = abs(x - Dx) + abs(y - Dy) - abs(T - s);
    // 当前位置到终点的所需要的时间大于剩下的时间
    // 奇偶性剪枝 ,起点和终点确定以后就可以确定走的步数是奇数还是偶数(没这个会超时滴%>_<%)
    if (tmp > 0 || tmp & 1) return false;
    rep(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
        if (vis[nx][ny] || G[nx][ny] == 'X') continue;
        vis[nx][ny] = true;
        if (dfs(nx, ny, s + 1)) return true;
        vis[nx][ny] = false;
    }
    return false;
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    while (~scanf("%d %d %d", &H, &W, &T) && H + W + T) {
        int tot = 0;
        rep(i, H) {
            scanf("%s", G[i]);
            rep(j, W) {
                vis[i][j] = false;
                if (G[i][j] == 'S') Sx = i, Sy = j;
                if (G[i][j] == 'D') Dx = i, Dy = j;
                if (G[i][j] == 'X') tot++;
            }
        }
        // 能走的格子个数比时间少的话,直接不符合,不用再搜了
        if (H * W - tot <= T) { puts("NO"); continue; }
        vis[Sx][Sy] = true;
        puts(dfs(Sx, Sy, 0) ? "YES" : "NO");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值