hdu 1010 Tempter of the Bone

原创 2015年07月08日 13:21:39

原题链接: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;
}

相关文章推荐

C语言DFS(5)____Tempter of the Bone(HDU 1010)

Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. Howev...

HDU1010 Tempter of the Bone(深度优先搜索DFS+奇偶性剪枝)

题目: Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav...

HDU1010 Tempter of the Bone 【DFS】+【剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O...

HDU 1010 Tempter of the Bone(DFS,奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...

hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)

本题链接:点击打开链接 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768...

HDU1010 Tempter of the Bone (DFS)

写的第二道DFS,这题在上ACM课的时候要求过,但是没好好去写,现在才第一次试着写,虽然还是要借鉴网上AC的代码,但是觉得自己对于如何写搜索有了一定的感受,这题的关键在于剪枝,奇偶性剪枝。 #incl...

【HDU 1010 Tempter of the Bone】 + DFS + 减枝

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other...

HDU 1010.Tempter of the Bone【DFS+奇偶性剪枝】9月19

Tempter of the Bone Problem Description The doggie found a bone in an ancient maze, which fa...

hdu1010Tempter of the Bone

1.题目:   Problem Description   The doggie found a bone in an ancient maze, which fascinated him a lot...

hdu 1010 Tempter of the Bone dfs+奇偶剪枝

H - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1010 Tempter of the Bone
举报原因:
原因补充:

(最多只允许输入30个字)