BFS

作业介绍

#include <bits/stdc++.h>
using namespace std;
const int N = 45;
int n,m,book[N][N],nxt[4][2]={0,1,0,-1,1,0,-1,0};
char mat[N][N];
struct node {
    int x,y,step;
};
int main() {
    cin>>n>>m;
    for (int i=1;i<=n;i++) {
        for (int j=1;j<=m;j++) {
            cin>>mat[i][j];
        }
    }
    queue<node>q;
    q.push({1,1,1});
    while (!q.empty()) {
        node tmp = q.front();
        q.pop();
        if (tmp.x==n && tmp.y==m) {
            cout<<tmp.step<<endl;
            return 0;
        }
        for (int i=0;i<4;i++) {
            int nx = tmp.x+nxt[i][0];
            int ny = tmp.y+nxt[i][1];
            if (nx>=1 && nx<=n && ny>=1 && ny<=m) {
                if (book[nx][ny]==0 && mat[nx][ny]!='#') {
                    q.push({nx,ny,tmp.step+1});
                    book[nx][ny] = 1;
                }
            }
        }
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int n,m,sx,sy,fx,fy;
char mat[N][N];
int book[N][N];//起点到ij点的花费
int nxt[4][2]={0,1,0,-1,1,0,-1,0};
struct node {
    int x,y,step;
};
int main() {
    cin>>n>>m;
    for (int i=1;i<=n;i++) {
        for (int j=1;j<=m;j++) {
            cin>>mat[i][j];
            if (mat[i][j]=='Z')sx=i,sy=j;
            if (mat[i][j]=='W')fx=i,fy=j;
        }
    }
    queue<node>q;
    q.push({sx,sy,0});
    memset(book,0x3f,sizeof(book));
    book[sx][sy] = 0;
    while (!q.empty()) {
        node tmp = q.front();
        q.pop();
        for (int i=0;i<4;i++) {
            int nx = tmp.x+nxt[i][0];
            int ny = tmp.y+nxt[i][1];
            if (nx>=1 && nx<=n & ny>=1 && ny<=m) {
                if (mat[nx][ny]!='#') {
                    int cost = tmp.step+1;
                    if (mat[nx][ny]>='1' && mat[nx][ny]<='9') {
                        cost+=mat[nx][ny]-'0';
                    }
                    if (cost<book[nx][ny]) {
                        book[nx][ny] = cost;
                        q.push({nx,ny,cost});
                    }
                }
            }
        }
    }
    cout<<book[fx][fy]<<endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int n,m,mat[N][N];
int p[250001][2],book[N][N],cnt,nxt[4][2]={0,1,0,-1,1,0,-1,0};
struct node {
    int x,y;
};
int check(int x) {
    memset(book,0,sizeof(book));
    queue<node>q;
    q.push({p[1][0],p[1][1]});
    book[p[1][0]][p[1][1]] = 1;
    while (!q.empty()) {
        node tmp = q.front();q.pop();
        for (int i=0;i<4;i++) {
            int nx = tmp.x+nxt[i][0];
            int ny = tmp.y+nxt[i][1];
            if (nx>=1 && nx<=n && ny>=1 && ny<=m) {
                if (book[nx][ny]==0 && abs(mat[nx][ny]-mat[tmp.x][tmp.y])<=x) {
                    book[nx][ny] = 1;
                    q.push({nx,ny});
                }
            }
        }
    }
    for (int i=1;i<=cnt;i++) {
        if (book[p[i][0]][p[i][1]]==0)return 0;
    }
    return 1;
}
int main() {
    cin>>n>>m;
    for (int i=1;i<=n;i++) {
        for (int j=1;j<=m;j++) {
            cin>>mat[i][j];
        }
    }
    for (int i=1;i<=n;i++) {
        for (int j=1;j<=m;j++) {
            int x;
            cin>>x;
            if (x==1) {
                ++cnt;
                p[cnt][0] = i;
                p[cnt][1] = j;
            }
        }
    }
    int l=0,r=1e9,res=-1;
    while (l<=r) {
        int mid = (l+r)>>1;
        if (check(mid)) {
            r = mid-1;
            res = mid;
        }
        else l=mid+1;
    }
    cout<<res<<endl;
    return 0;
}
状态
已结束
题目
16
开始时间
2026-8-3 0:00
截止时间
2026-8-11 23:59
可延期
24 小时