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;
}
相关推荐
我想进大厂40 分钟前
图论---朴素Prim(稠密图)
数据结构·c++·算法·图论
我想进大厂1 小时前
图论---Bellman-Ford算法
数据结构·c++·算法·图论
AIGC大时代1 小时前
高效使用DeepSeek对“情境+ 对象 +问题“型课题进行开题!
数据库·人工智能·算法·aigc·智能写作·deepseek
CODE_RabbitV1 小时前
【深度强化学习 DRL 快速实践】近端策略优化 (PPO)
算法
Wendy_robot2 小时前
【滑动窗口+哈希表/数组记录】Leetcode 438. 找到字符串中所有字母异位词
c++·算法·leetcode
程序员-King.2 小时前
day49—双指针+贪心—验证回文串(LeetCode-680)
算法·leetcode·贪心算法·双指针
转基因3 小时前
Codeforces Round 1020 (Div. 3)(题解ABCDEF)
数据结构·c++·算法
我想进大厂4 小时前
图论---Kruskal(稀疏图)
数据结构·c++·算法·图论
@Aurora.4 小时前
数据结构手撕--【二叉树】
数据结构·算法
victd4 小时前
什么是AutoRec?
算法