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;
}
相关推荐
学不动CV了2 小时前
C语言32个关键字
c语言·开发语言·arm开发·单片机·算法
小屁孩大帅-杨一凡2 小时前
如何解决ThreadLocal内存泄漏问题?
java·开发语言·jvm·算法
Y1nhl4 小时前
力扣_二叉树的BFS_python版本
python·算法·leetcode·职场和发展·宽度优先
向阳逐梦5 小时前
PID控制算法理论学习基础——单级PID控制
人工智能·算法
2zcode5 小时前
基于Matlab多特征融合的可视化指纹识别系统
人工智能·算法·matlab
Owen_Q6 小时前
Leetcode百题斩-二分搜索
算法·leetcode·职场和发展
矢志航天的阿洪6 小时前
蒙特卡洛树搜索方法实践
算法
UnderTheTime6 小时前
2025 XYD Summer Camp 7.10 筛法
算法
zstar-_6 小时前
Claude code在Windows上的配置流程
笔记·算法·leetcode
圆头猫爹7 小时前
第34次CCF-CSP认证第4题,货物调度
c++·算法·动态规划