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;
}
相关推荐
要开心吖ZSH4 分钟前
软件设计师备考-(十六)数据结构及算法应用(重要)
java·数据结构·算法·软考·软件设计师
带娃的IT创业者17 分钟前
如何开发一个教育性质的多线程密码猜测演示器
网络·python·算法
Aczone282 小时前
硬件(六)arm指令
开发语言·汇编·arm开发·嵌入式硬件·算法
luckys.one6 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
~|Bernard|8 小时前
在 PyCharm 里怎么“点鼠标”完成指令同样的运行操作
算法·conda
战术摸鱼大师8 小时前
电机控制(四)-级联PID控制器与参数整定(MATLAB&Simulink)
算法·matlab·运动控制·电机控制
Christo38 小时前
TFS-2018《On the convergence of the sparse possibilistic c-means algorithm》
人工智能·算法·机器学习·数据挖掘
好家伙VCC9 小时前
数学建模模型 全网最全 数学建模常见算法汇总 含代码分析讲解
大数据·嵌入式硬件·算法·数学建模
liulilittle10 小时前
IP校验和算法:从网络协议到SIMD深度优化
网络·c++·网络协议·tcp/ip·算法·ip·通信
bkspiderx12 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法