BFS之最短路径模型

当一个图的每个边的权重都一样的时候,会有一个最短路径模型。不需要考虑边的影响。

1076. 迷宫问题 - AcWing题库

cpp 复制代码
#include<iostream>
#include<queue>
#include<utility>
#include<algorithm>
#include<stack>
using namespace std;
const int N = 1010;
typedef pair<int,int> PII;
int g[N][N];  //存储地图
bool st[N][N]; //存储状态
PII pre[N][N];//存储前一个点
int n;
PII track[N];
int k=0;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};


void bfs(int sx,int sy){
    queue<PII> q;
    q.push(make_pair(sx,sy));
    st[sx][sy] = true;
    while(!q.empty()){
        PII t = q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int nx = t.first + dx[i];
            int ny = t.second + dy[i];
            if(nx >=0 && nx<n && ny>=0 && ny<n && g[nx][ny]==0 && st[nx][ny]==false){
                pre[nx][ny] = t;
                q.push(make_pair(nx,ny));
                st[nx][ny] = true;
            }
            // if(nx<0 || nx>n-1 || ny<0 || ny>n-1) continue;
            // if(st[nx][ny]) continue;
            // if(g[nx][ny]==1) continue;
            // pre[nx][ny] = t;
            // q.push(make_pair(nx,ny));
            // st[nx][ny] = true;
        }
        
    }
}

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>g[i][j];
            pre[i][j] = {-1, -1};
        }
    }
    
    bfs(0,0);
    stack<PII> s;
    PII present = pre[n-1][n-1];
    while(true){
        s.push(present);
        if(present.first==0 && present.second==0) break;
        present = pre[present.first][present.second];
    }
    while(!s.empty()){
        PII v = s.top();
        s.pop();
        cout<<v.first<<" "<<v.second<<endl;
    }
    cout<<n-1<<" "<<n-1<<endl;


    
    
    return 0;
}

马走日模型

188. 武士风度的牛 - AcWing题库

cpp 复制代码
#include<iostream>
#include<utility>
#include<queue>
#include<algorithm>
using namespace std;
typedef pair<int,int> PII;
PII start,end;
const int N = 160;
int n,m,res=-1;
char g[N][N];
bool st[N][N];
bool flag;
int dist[N][N];
int dx[] = {-2,-1,1,2,2,1,-1,-2};
int dy[] = {1,2,2,1,-1,-2,-2,-1};

void bfs(int sx,int sy){
    queue<PII> q;
    q.push(make_pair(sx,sy));
    st[sx][sy] = true;
    while(!q.empty()){
        if(flag) break;
        PII t = q.front();
        q.pop();
        for(int i=0;i<8;i++){
            int nx = t.first + dx[i];
            int ny = t.second + dy[i];
            if(nx <1 || nx >n || ny <1 || ny>m) continue;//不能越界
            if(st[nx][ny]) continue;//遍历过的不能再次遍历
            if(g[nx][ny]=='*') continue;//不能在*上面
            if(g[nx][ny]=='H'){  //走到有草的地方了,立即暂停并打印
                cout<<dist[t.first][t.second]+1;
                flag = true;
                break;
            }
            q.push(make_pair(nx,ny));
            st[nx][ny] = true;
            dist[nx][ny] = dist[t.first][t.second]+1;
        }
    }
}

int main(){
    
    cin>>m>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>g[i][j];
            if(g[i][j]=='K'){
                start.first = i;
                start.second = j;
            }
        }
    }
    
    // for(int i=1;i<=n;i++){  //检查输入
    //     for(int j=1;j<=m;j++){
    //         cout<<g[i][j];
    //     }
    //     cout<<endl;
    // }
    bfs(start.first,start.second);
    
    
    return 0;
}
相关推荐
NAGNIP16 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队17 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja21 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下21 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶1 天前
算法 --- 字符串
算法
博笙困了1 天前
AcWing学习——差分
c++·算法
NAGNIP1 天前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP1 天前
大模型微调框架之LLaMA Factory
算法
echoarts1 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Python技术极客1 天前
一款超好用的 Python 交互式可视化工具,强烈推荐~
算法