zoukankan      html  css  js  c++  java
  • HDU 1010 Tempter of the Bone (DFS+剪枝)

    题意:从S走到D,能不能恰好用T时间。

    析:这个题时间是恰好,并不是少于T,所以用DFS来做,然后要剪枝,不然会TEL,我们这样剪枝,假设我们在(x,y),终点是(ex,ey),

    那么从(x, y)到(ex, ey),要么时间正好是T-你已经走过的时间,要么要向别的地方先拐一下,以凑出这个正好时间,既然要拐一下,那么一定要回来,

    所以时间肯定得是偶数,要不然完不成(回不来),

    所以(t - abs(ex-x) - abs(ey-y) - cnt ),如果是奇数就剪枝。然而用C++交就TLE,用G++就AC。。。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    using namespace std ;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f3f;
    const double eps = 1e-8;
    const int maxn = 1e4 + 5;
    const int mod = 1e9 + 7;
    const int dr[] = {0, 0, -1, 1};
    const int dc[] = {-1, 1, 0, 0};
    int n, m;
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    int t;
    char s[10][10];
    int vis[10][10];
    int sx, sy, ex, ey;
    
    
    bool dfs(int r, int c, int cnt){
        if(cnt > t)  return false;
        if(r == ex && c == ey && cnt == t)  return true;
        if((t - abs(ex-r) - abs(ey-c) - cnt) & 1)  return false;
    
        for(int i = 0; i < 4; ++i){
            int x = dr[i] + r;
            int y = dc[i] + c;
            if(!is_in(x, y) || vis[x][y] || s[x][y] == 'X')  continue;
            vis[x][y] = 1;
            if(x == ex && ey == y && cnt + 1 == t)  return true;
            if((t - abs(ex-x) - abs(ey-y) - cnt -1) & 1) continue;
            if(dfs(x, y, cnt+1))  return true;
            vis[x][y] = 0;
        }
        return false;
    }
    
    int main(){
        while(scanf("%d %d %d", &n, &m, & t) == 3){
            if(!n && !m && !t)  break;
            for(int i = 0; i < n; ++i)
                scanf("%s", s[i]);
            for(int i = 0; i < n; ++i)
                for(int j = 0; j < m; ++j)
                    if(s[i][j] == 'S')  sx = i, sy = j;
                    else if(s[i][j] == 'D')  ex = i, ey = j;
            memset(vis, 0, sizeof(vis));
            vis[sx][sy] = 1;
            if(dfs(sx, sy, 0)) puts("YES");
            else  puts("NO");
        }
        return 0;
    }
    
  • 相关阅读:
    [BJOI2019] 光线
    C# 从零开始写 SharpDx 应用 笔刷
    BAT 脚本判断当前系统是 x86 还是 x64 系统
    BAT 脚本判断当前系统是 x86 还是 x64 系统
    win2d 通过 CanvasActiveLayer 画出透明度和裁剪
    win2d 通过 CanvasActiveLayer 画出透明度和裁剪
    PowerShell 拿到显卡信息
    PowerShell 拿到显卡信息
    win10 uwp 如何使用DataTemplate
    win10 uwp 如何使用DataTemplate
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5743556.html
Copyright © 2011-2022 走看看