hdu 1010 Tempter of the Bone

来源:互联网 发布:250bp进入编程 编辑:程序博客网 时间:2024/05/22 07:00

原题链接: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;}
0 0
原创粉丝点击