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;
}
相关推荐
燕双嘤1 分钟前
Require:基于雪花算法完成一个局部随机,全局离散没有热点切唯一的数值Id生成器。
算法·dreamweaver
了无痕131417 分钟前
8-回溯算法
算法
问道飞鱼29 分钟前
每日学习一个数据结构-堆
数据结构·学习·算法
ad_l1 小时前
代码随想录_刷题记录_第四次
笔记·算法·leetcode
cdut_suye1 小时前
STL之list篇(上)初识list容器,了解其核心机制,实例化对象进行分析
开发语言·c++·学习·算法·stl·list
GISer Liu1 小时前
LeetCode从入门到超凡(四)深入浅出理解贪心算法
python·算法·leetcode·面试·职场和发展·贪心算法·datawhale
多多*2 小时前
OJ在线评测系统 前端开发整合开源组件 Monaco Editor 并且开发创建题目页面
服务器·前端·javascript·数据库·算法·开源
真忒修斯之船2 小时前
每天3分钟,彻底弄懂神经网络的优化器(三)Momentum
人工智能·算法·面试
谢尔登2 小时前
【Webpack】Hash 码
算法·webpack·哈希算法
程序和我有一个能跑就行。2 小时前
【Python】递归
数据结构·python·算法·递归