zoukankan      html  css  js  c++  java
  • UVA 1600 Patrol Robot (巡逻机器人)(bfs)

    题意:从(1,1)走到(m,n),最多能连续穿越k个障碍,求最短路。

    分析:obstacle队列记录当前点所穿越的障碍数,如果小于k可继续穿越障碍,否则不能,bfs即可。

    #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)
    typedef long long ll;
    typedef unsigned long long llu;
    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, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const double eps = 1e-8;
    const int MAXN = 20 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int pic[MAXN][MAXN];
    int vis[MAXN][MAXN];
    int m, n, k;
    bool judge(int x, int y){
        return x >= 1 && x <= m && y >= 1 && y <= n;
    }
    int bfs(){
        queue<int> x, y, step, obstacle;
        x.push(1);
        y.push(1);
        step.push(0);
        obstacle.push(0);
        vis[1][1] = 1;
        while(!x.empty()){
            int tmpx = x.front(); x.pop();
            int tmpy = y.front(); y.pop();
            int tmpstep = step.front(); step.pop();
            int tmpobstacle = obstacle.front(); obstacle.pop();
            for(int i = 0; i < 4; ++i){
                int tx = tmpx + dr[i];
                int ty = tmpy + dc[i];
                if(judge(tx, ty) && !vis[tx][ty]){
                    if(tx == m && ty == n) return tmpstep + 1;
                    if(pic[tx][ty] == 0){
                        vis[tx][ty] = 1;
                        x.push(tx);
                        y.push(ty);
                        step.push(tmpstep + 1);
                        obstacle.push(0);
                    }
                    else if(pic[tx][ty] == 1){
                        int nowobstacle = tmpobstacle + 1;
                        if(nowobstacle <= k){
                            x.push(tx);
                            y.push(ty);
                            step.push(tmpstep + 1);
                            obstacle.push(nowobstacle);
                        }
                    }
                }
            }
        }
        return -1;
    }
    int main(){
        int T;
        scanf("%d", &T);
        while(T--){
            memset(pic, 0, sizeof pic);
            memset(vis, 0, sizeof vis);
            scanf("%d%d%d", &m, &n, &k);
            for(int i = 1; i <= m; ++i){
                for(int j = 1; j <= n; ++j){
                    scanf("%d", &pic[i][j]);
                }
            }
            int ans = bfs();
            printf("%d\n", ans);
        }
        return 0;
    }
  • 相关阅读:
    MaltReport2:通用文档生成引擎
    PostgreSQL 10 如何使用 PgAdmin3
    Stackoverflow 珠玑:C#封装重试指定次数的功能
    C# 6 元组应用 Part 2:C# 也玩模式匹配
    C# 6 元组应用 Part 1:方便的字典工厂方法
    Stackoverflow 珠玑:用于分组的 LINQ 扩展方法
    Linux 下的 PostgreSQL 数据库+文件通用自动备份脚本
    让 Odoo POS 支持廉价小票打印机
    NopCommerce 根据手机浏览器和桌面浏览器切换 Theme
    为什么 C# 比 C++ 编译快那么多
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6282044.html
Copyright © 2011-2022 走看看