BFS 算法提高课

池塘计数

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
char g[N][N];
bool st[N][N];
int n,m;
int cnt;
int dist[N][N];
int dx[]={0,0,-1,1,1,1,-1,-1},dy[]={-1,1,0,0,1,-1,-1,1};
void bfs(int x,int y){
    queue<pair<int,int>>q;
    q.push({x,y});
    st[x][y]=true;
    while(q.size()){
        auto t=q.front();
        q.pop();
        for(int i=0;i<8;i++){
            int xx=t.first+dx[i],yy=t.second+dy[i];
            if(xx>n||xx<1||yy>m||y<1) continue;
            if(st[xx][yy]||g[xx][yy]!='W') continue;
            st[xx][yy]=true;
            q.push({xx,yy});
        }
    }
}
int main(){
    cin >> n >> m ;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin >> g[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(!st[i][j]&&g[i][j]=='W'){
                bfs(i,j);
                cnt++;
            }
        }
    }
    cout << cnt;
    return 0;
}

城堡计数

#include<bits/stdc++.h>
using namespace std;
const int N=55;
int g[N][N];
bool st[N][N];
int n,m;
int ans;
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int bfs(int x,int y){
    int num=1;
    st[x][y]=true;
    queue<pair<int,int>>q;
    q.push({x,y});
    while(q.size()){
        auto t=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int xx=t.first+dx[i],yy=t.second+dy[i];
            if((g[t.first][t.second]>>i) & 1 ) continue;//二进制表示计墙可以参考下面博客
            if(st[xx][yy]) continue;
            st[xx][yy]=true;
            q.push({xx,yy});
            num++;
        }
    }
    return num;
}
int main(){
    int cnt=0;
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin >> g[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(!st[i][j]){
                cnt=max(cnt,bfs(i,j));
                ans++;
            }
        }
    }
    cout << ans << endl <<cnt;
    return 0;
}

参考题解

山峰和山谷

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int g[N][N];
bool st[N][N];
int n;
bool higher,lower;
int peak;//山峰
int valley; // 山谷
int dx[]={-1,1,0,0,1,1,-1,-1},dy[]={0,0,-1,1,1,-1,1,-1};
void bfs(int x,int y,bool& higher, bool& lower){
    queue<pair<int,int>>q;
    q.push({x,y});
    while(q.size()){
        auto t=q.front();
        q.pop();
        for(int i=0;i<8;i++){
            int xx=t.first+dx[i],yy=t.second+dy[i];
            if(xx>n||xx<1||yy>n||yy<1) continue;
            if(g[x][y]<g[xx][yy]) higher=true;
            if(g[x][y]>g[xx][yy]) lower=true;
            if(g[x][y]==g[xx][yy]&&!st[xx][yy]){
                q.push({xx,yy});
                st[xx][yy]=true;
            }
        }
    }
}
int main(){
    cin >> n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin >> g[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(!st[i][j]){
                higher=lower=false;
                st[i][j]=true;
                bfs(i,j,higher,lower);
                if(higher==true&&lower==true){
                    continue;
                }
                if(higher==false&&lower==false){
                    peak++,valley++;
                    continue;
                }
                if(higher==true){
                    valley++;
                    continue;
                }
                if(lower==true){
                    peak++;
                }
            }
        }
    }
    cout << peak << " " << valley;
    return 0;
}

迷宫问题

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int g[N][N];
bool st[N][N];
pair<int,int> pre[N][N];
int dist[N][N];
int dx[]={0,0,-1,1},dy[]={1,-1,0,0};
int n;
void bfs(int x,int y){
    st[x][y]=true;
    queue<pair<int,int>>q;
    q.push({x,y});
    while(q.size()){
        auto t=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int xx=t.first+dx[i],yy=t.second+dy[i];
            if(xx>n||xx<1||yy>n||yy<1||st[xx][yy]||g[xx][yy]==1) continue;
            dist[xx][yy]=dist[t.first][t.second]+1;
            st[xx][yy]=true;
            pre[xx][yy]={t.first,t.second};
            q.push({xx,yy});
        }
    }
}
int main(){
    cin >> n ;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin >> g[i][j];
        }
    }
    bfs(n,n);
    auto t=pre[1][1];
    cout << 0 << " " << 0 <<endl;
    while(1){
       cout << t.first-1 << " " << t.second-1 << endl;
       if(t.first==n&&t.second==n){
           break;
       }
       t=pre[t.first][t.second];
    }
    return 0;
}

抓住那头牛

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
bool st[N*2];
int dist[N*2];
int sta,ed;
void bfs(){
    queue<int>q;
    q.push(sta);
    st[sta]=true;
    while(q.size()){
        auto t=q.front();
        q.pop();
        if(t==ed){
            break;
        }
        if(t-1>=0&&!st[t-1]){
            st[t-1]=true;
            q.push(t-1);
            dist[t-1]=dist[t]+1;
        }
        if(t+1<=ed&&!st[t+1]){
            st[t+1]=true;
            q.push(t+1);
            dist[t+1]=dist[t]+1;
        }
        if(t*2<=2*ed&&!st[t*2]){
            st[t*2]=true;
            q.push(t*2);
            dist[t*2]=dist[t]+1;
        }
    }
}
int main(){
    cin >> sta >> ed;
    bfs();
    cout << dist[ed];
    return 0;
}

武士风度的牛

#include<bits/stdc++.h>
using namespace std;
const int N=155;
char g[N][N];
bool st[N][N];
int dist[N][N];
int n,m;
int stx,sty;
int endx,endy;
int ans=0;
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};

void bfs(){
    queue<pair<int,int>>q;
    q.push({stx,sty});
    st[stx][sty]=true;
    while(q.size()){
        auto t=q.front();
        if(g[t.first][t.second]=='H') return;
        q.pop();
        for(int i=0;i<8;i++){
            int x=t.first+dx[i],y=t.second+dy[i];
            if(st[x][y]||g[x][y]=='*') continue;
            if(x<1||x>n||y<1||y>m) continue;
            st[x][y]=true;
            dist[x][y]=dist[t.first][t.second]+1;
            q.push({x,y});
        }
    }
}
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'){
                 stx=i;
                sty=j;
            }
            if(g[i][j]=='H'){
                endx=i;
                endy=j;
            }
        }
    }
    bfs();
    cout <<dist[endx][endy];
    return 0;
}

矩阵距离

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
char g[N][N];
bool st[N][N];
int n,m;
int dist[N][N];
queue<pair<int,int>>q;
int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
void bfs(){
    while(q.size()){
        auto t=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int x=t.first+dx[i],y=t.second+dy[i];
            if(x>n||x<1||y>m||y<1) continue;
            if(st[x][y]) continue;
            dist[x][y]=dist[t.first][t.second]+1;
            st[x][y]=true;
            q.push({x,y});
        }
    }
}
int main(){
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        string a;
        cin >> a;
        for(int j=0;j<a.size();j++){
            if(a[j]=='1'){
                q.push({i,j+1});
                st[i][j+1]=true;
            }
        }
    }
    bfs();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout << dist[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

魔板//注意代码的实现方式


#include<bits/stdc++.h>
using namespace std;
unordered_map<string,int> dist;
unordered_map<string,pair<char,string>>pre;
string st="",ed="";
string move1(string t){
    for(int i=0;i<4;i++) swap(t[i],t[7-i]);
    return t;
}
string move2(string t){
    for(int i=0;i<3;i++) swap(t[3],t[i]);
    for(int i=4;i<7;i++) swap(t[i],t[i+1]);
    return t;
}
string move3(string t){
    swap(t[1],t[2]),swap(t[5],t[6]),swap(t[1],t[5]);
    return t;
}
void bfs(){
    queue<string>q;
    q.push(st);
    while(q.size()){
        auto t=q.front();
        q.pop();
        if(t==ed){
            break;
        }
        string m[3];
        m[0]=move1(t);
        m[1]=move2(t);
        m[2]=move3(t);
        for(int i=0;i<3;i++){
            if(!dist.count(m[i])){
                q.push(m[i]);
                dist[m[i]]=dist[t]+1;//eg:dist[abc]=dist[cba]+1;dist[cba]=0;
                pre[m[i]]={'A'+i,t};//pre[abc]={A,cba}; 只有char类型才能'A'加上一个东西
                //t 经过  'A'+i  变化 变成m[i]
            }
        }
    }
}
int main(){
    for(int i=1;i<=8;i++){
        char b=(i+'0');
        st=st+b;
        char a;
        cin >> a;
        ed=ed+a;
    }
    bfs();
    cout << dist[ed] << endl;
    string res="";
     if(dist[ed])
    {
        while(ed!=st)
        {
            res+=pre[ed].first;
            ed=pre[ed].second;
        }
        reverse(res.begin(),res.end());
        cout<< res <<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值