zoukankan      html  css  js  c++  java
  • POJ 3009 Curling 2.0(dfs)

    题意:一个球从起点开始可以沿上下左右四个方向走,若往一个方向走,会有三种情况:

    1、沿着该方向,一直到遇到障碍物才会停下(停在面前是障碍物的地方),且会把该障碍物撞成空地。

    2、沿着该方向,一直到经过终点,游戏胜利。

    2、沿着该方向,一直到走出游戏边界,游戏结束。

    问是否能在十步之内从起点到终点。(沿着某个方向一直走下去直到遇到上述三种情况,算一步)

    分析:

    1、dfs即可。注意选择方向之前,首先判断该方向的下一步是否是空地,是否出界。

    2、起点和终点也是可以经过的空地。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, -1, 0, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 0, 1, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 20 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int pic[MAXN][MAXN];
    int tmp[MAXN][MAXN];
    int maxn;
    int w, h;
    int sx, sy, ex, ey;
    bool judge(int x, int y){
        return x >= 0 && x < h && y >= 0 && y < w;
    }
    bool dfs(int x, int y, int cur){
        if(cur == maxn) return false;
        for(int i = 0; i < 4; ++i){
            int tmpx = x + dr[i];
            int tmpy = y + dc[i];
            if(judge(tmpx, tmpy) && !tmp[tmpx][tmpy]){
                while(judge(tmpx, tmpy) && !tmp[tmpx][tmpy]){
                    if(tmpx == ex && tmpy == ey){
                        return true;
                    }
                    tmpx = tmpx + dr[i];
                    tmpy = tmpy + dc[i];
                }
                if(judge(tmpx, tmpy)){
                    tmp[tmpx][tmpy] = 0;
                    if(dfs(tmpx - dr[i], tmpy - dc[i], cur + 1)) return true;
                    tmp[tmpx][tmpy] = 1;
                }
            }
        }
        return false;
    }
    int main(){
        while(scanf("%d%d", &w, &h) == 2){
            if(!w && !h) return 0;
            for(int i = 0; i < h; ++i){
                for(int j = 0; j < w; ++j){
                    scanf("%d", &pic[i][j]);
                    if(pic[i][j] == 2){
                        sx = i;
                        sy = j;
                        pic[i][j] = 0;
                    }
                    else if(pic[i][j] == 3){
                        ex = i;
                        ey = j;
                        pic[i][j] = 0;
                    }
                }
            }
            bool ok = false;
            for(maxn = 1; maxn <= 10; ++maxn){
                memcpy(tmp, pic, sizeof pic);
                if(dfs(sx, sy, 0)){
                    ok = true;
                    printf("%d\n", maxn);
                    break;
                }
            }
            if(!ok){
                printf("-1\n");
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    更改已经签名的app中的内容
    POJ 1611 The Suspects
    hibernate session缓存
    男儿当自强
    图遍历的演示
    nginx源代码分析--事件模块 &amp; 琐碎
    多线程在python中的使用 thread
    机房收费 &amp; 廊院食堂
    the steps that may be taken to solve a feature selection problem:特征选择的步骤
    早绑定和迟绑定技术的通俗分析
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6566996.html
Copyright © 2011-2022 走看看