Patrol Robot UVA - 1600 BFS最短路径长

题目链接

题目大意:

给一个矩阵,从(1,1)走到(m,n)的最短路,"1"是障碍,不能连续穿过k个障碍。

分析:

用一个结构体,属性有x,y坐标,当前距离,可以跨越障碍数目。

然后bfs便利即可。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 30;
int dir[5][5] = {{1,0},{0,1},{-1,0},{0,-1}};
struct Node
{
    int x,y,dis,step;
    Node(int _x, int _y, int _dis,  int _step):
        x(_x), y(_y), dis(_dis), step(_step) {};
};
int matrix[maxn][maxn],r,c,num;
bool vis[maxn][maxn];
int bfs() {
    queue<Node> queue1;
    queue1.push(Node(0,0,0,num));
    while(!queue1.empty()) {
        Node u = queue1.front(); queue1.pop();
        int ur = u.x, uc = u.y;
        if(ur==r-1 && uc==c-1) return u.dis;
        for(int i = 0; i < 4; i++) {
            int vr = ur+dir[i][0], vc = uc+dir[i][1];
            if(vr>=0 && vc>=0 && vr<r && vc<c) {
                if(!matrix[vr][vc] && !vis[vr][vc]) {
                    vis[vr][vc] = true;
                    queue1.push(Node(vr,vc,u.dis+1,num));
                }
                if(matrix[vr][vc]==1 && u.step>0 && !vis[vr][vc]) {
                    vis[vr][vc] = true;
                    queue1.push(Node(vr,vc,u.dis+1,u.step-1));
                }
            }
        }
    }
    return -1;
}


int main() {
    freopen("i.txt","r",stdin);
    freopen("o.txt","w",stdout);
    int n;
    cin >> n;
    while(n--) {
        memset(matrix,0,sizeof(matrix));
        memset(vis,false,sizeof(vis));
        cin >> r >> c >> num;
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++)
                cin >> matrix[i][j];
        }
        cout << bfs() << endl;
    }
}

第一次用数组写的,总是wa,不知道为什么,先贴着吧。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 50;
int dir[5][5] = {{1,0},{0,1},{-1,0},{0,-1}};
int matrix[maxn][maxn], dis[maxn*30+maxn], vis[maxn*30+maxn];
int jump[maxn*30+maxn];
int r,c,num;

void bfs() {
    queue<int> queue1;
    int beg = 0;
    queue1.push(beg);
    dis[beg] = 0; vis[beg] = 1;
    while(!queue1.empty()) {
        int u = queue1.front(); queue1.pop();
        int rr = u/30, cc = u%30;
        for(int i = 0; i < 4; i++) {
            int vr = rr+dir[i][0], vc = cc+dir[i][1];
            if(vr>=0 && vr<r && vc>=0 && vc<c) {
                bool flag = false;
                int v = vr*30+vc;
                if(!matrix[vr][vc]) {
                    flag = true;
                    jump[v] = num;
                }
                else if(jump[u]>0) {
                    flag = true;
                    jump[v] = jump[u]-1;
                }
                if(flag && !vis[v]) {
                    queue1.push(v);
                    vis[v] = 1;
                    dis[v] = dis[u]+1;
                }
            } 
        }
    }
}

int main() {
    int n;
    cin >> n;
    while(n--) {
        memset(matrix, 0, sizeof(matrix));
        memset(dis, -1, sizeof(dis));
        memset(vis, 0, sizeof(vis));
        cin >> r >> c >> num;
        for(int i = 0; i < 30*r+c; i++)
            jump[i] = num;
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) 
                cin >> matrix[i][j];
        }
        bfs();
        int ed = (r-1)*30+(c-1);
        cout << dis[ed] << endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值