UVA1600 Patrol Robot(BFS)

本题vjudge链接

  • BFS进阶题
  • 题意:有个移动的机器人,从(1, 1)点出发,目标是(m, n)点,0为可以通过的点,1为障碍点,机器人一次最多只能穿越k障碍,问你最少要走多少步到达终点
  • BFS走,要注意个是要记录到达障碍点的最短穿越障碍个数
#include <bits/stdc++.h>

using namespace std;

int n, m, g[25][25], t, k;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

inline bool check(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= m; }

struct coo{
    int x, y, d, k;
    coo(){}
    coo(int x, int y, int d, int k) : x(x), y(y), d(d), k(k){}
};

void bfs() {
    queue<coo> q;
    bool vis[25][25][25] = {0};
    vis[1][1][0] = g[1][1] == 1;
    q.push(coo(1, 1, 0, g[1][1] == 1));
    while (q.size()) {
        coo u = q.front();
        q.pop();
        if (vis[u.y][u.x][u.k]) continue;
        vis[u.y][u.x][u.k] = true;
        if (u.x == n && u.y == m) { printf("%d\n", u.d); return; }
        for (int i = 0; i < 4; i++) {
            int x = u.x + dx[i], y = u.y + dy[i];
            if (!check(x, y)) continue;
            if (g[y][x]) {
                if (u.k + 1 > k) continue;//如果穿越障碍个数比之前算的还大就不计入
                q.push(coo(x, y, u.d + 1, u.k + 1));
            } else {
                q.push(coo(x, y, u.d + 1, 0));
            }
        }
    }
    puts("-1");
}

int main () {
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d", &m, &n, &k);
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                scanf("%d", &g[i][j]);
        bfs();
        memset(g, 0, sizeof g);
    }
    return 0;
}

原地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值