bfs之加强版

· · 个人记录

在之前的题目中,我们的 BFS 都是干货。直接上不就完事了?在今天的题目中,难度加大了,而且需要你找规律,从而找到搜索的奥秘。

P1379 八数码难题

题目描述

在 3\times 3 的棋盘上,摆有八个棋子,每个棋子上标有 1 至 8 的某一数字。棋盘中留有一个空格,空格用 0 来表示。空格周围的棋子可以移到空格中。要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为 123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变。

输入 #1
283104765

输出 #1

4

样例解释

图中标有 0 的是空格。绿色格子是空格所在位置,橙色格子是下一步可以移动到空格的位置。如图所示,用四步可以达到目标状态。

并且可以证明,不存在更优的策略。

解题思路:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
struct node{
    string s;
    int step;
};
int f[]={3,-3,-1,1};
map<string,bool>vis;
string a;
int bfs(){
    queue<node>q;
    q.push({a,0});
    vis[a]=true;
    while(!q.empty()){
        node t=q.front();
        if(t.s=="123804765"){//找到答案
            return t.step;
        }
        q.pop();
        string b=t.s;
        int xx=0;
        for(int i=0;i<b.size();i++){
            if(b[i]=='0'){//找出0出现的位置
                xx=i;
                break;
            }
        }
        for(int i=0;i<4;i++){
            int nx=f[i]+xx;
            string tt=t.s;
            swap(tt[nx],tt[xx]);//和0交换,出现新的字符串,相当于扩散操作。
//一坨不合法条件,恶心
            if(xx==2&&f[i]==1){
                continue;
            }
            if(xx==5&&f[i]==1){
                continue;
            }
            if(xx==8&&f[i]==1){
                continue;
            }
            if(xx==3&&f[i]==-1){
                continue;
            }
            if(xx==6&&f[i]==-1){
                continue;
            }
            if(nx>=0&&nx<9&&!vis[tt]){//判断是否符合要求
                vis[tt]=true;
                q.push({tt,t.step+1});
            }
        }
    }
}
signed main(){
    cin>>a;
    cout<<bfs();
    return 0;
}

P2730 [USACO3.2] 魔板 Magic Square

呜呜呜,这题太恶心了。

和上一题差不多。我们扩散的还是字符串。我们可以把题目给我们的三种操作进行模拟,如果没有出现过这种状态,就装进队列即可。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
struct node{
    string s;
    int step;
    string ans;
};
map<string,bool>vis;
string a;
void bfs(){
    queue<node>q;
    q.push({"12345678",0,""});
    vis["12345678"]=true;
    while(!q.empty()){
        node t=q.front();
        if(t.s==a){//找到答案
            cout<<t.step<<"\n";
            cout<<t.ans;
            return ;
        }
        string abc=t.s;
        q.pop();
        for(int i=0;i<3;i++){//这三种操作可以疯狂swap
            if(i==0){//A操作
                string tt=abc;
                swap(tt[0],tt[7]);
                swap(tt[1],tt[6]);
                swap(tt[2],tt[5]);
                swap(tt[3],tt[4]);
                if(!vis[tt]){
                    vis[tt]=true;
                    q.push({tt,t.step+1,t.ans+'A'});
                } 
            }
            if(i==1){//B操作
                string tt=abc;
                swap(tt[3],tt[2]);
                swap(tt[2],tt[1]);
                swap(tt[1],tt[0]);
                swap(tt[4],tt[5]);
                swap(tt[5],tt[6]);
                swap(tt[6],tt[7]);
                if(!vis[tt]){
                    vis[tt]=true;
                    q.push({tt,t.step+1,t.ans+'B'});
                } 
            }
            if(i==2){//C 操作
                string tt=abc;
                swap(tt[2],tt[5]);
                swap(tt[2],tt[1]);
                swap(tt[1],tt[6]);
                if(!vis[tt]){
                    vis[tt]=true;
                    q.push({tt,t.step+1,t.ans+'C'});
                } 
            }
        }
    }
    return ;
}
signed main(){
    char ch;
    while(cin>>ch){
        if(ch!=' '){
            a+=ch;
        }
    }
    bfs();
    return 0;
}

家庭作业

T582141 星际跃迁追踪协议

这题相比前面的题目,要简单一些,因为他是一维的,而且操作方式已经告诉你了。我们只需要每次取出队首后判断 y-1,y+1,2y 这几个位置是否合法,如果合法,装进队列并标记即可。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=4e5+6;
bool vis[maxn];
struct node{
    int x;
    int step;
};
int a,b;
int bfs(int x,int y){
    queue<node>q;
    q.push({x,0});
    vis[x]=true;
    while(!q.empty()){
        node t=q.front();
        q.pop();
        if(t.x==y){
            return t.step;
        }
//能走的条件:在边界之内,没走过
        if(t.x-1>=1&&t.x-1<=maxn&&!vis[t.x-1]){///不确定上界,所以用maxn
            vis[t.x-1]=true;
            q.push({t.x-1,t.step+1});
        }
        if(t.x+1>=1&&t.x+1<=maxn&&!vis[t.x+1]){//同上
            vis[t.x+1]=true;
            q.push({t.x+1,t.step+1});
        }
        if(t.x*2>=1&&t.x*2<=maxn&&!vis[t.x*2]){
            vis[t.x*2]=true;
            q.push({t.x*2,t.step+1});
        }
    }
    return 0;
}
signed main(){
    cin>>a>>b;
    cout<<bfs(a,b);
    return 0;
}

P1135 奇怪的电梯

这题和上一题思路一样!只是把 y-1,y+1,2y 换成了 y+a_x,y-a_x。就是上 a_x 楼和下 a_x 楼,同样,看合不合法即可。记得标记。这就没啥好说的了。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
int k[maxn];
struct node{
    int x,step;
};
int n,a,b;
bool vis[maxn];
int ans;
bool check(int x){
    if(x<0||x>maxn){
        return false;
    }
    if(vis[x]){
        return false;
    }
    return true;
}
int bfs(){
    queue<node>q;
    q.push({a,0});
    while(!q.empty()){
        node t=q.front();
        if(t.x==b){
            return t.step;
        }
        q.pop();
        int x=t.x,y;
        y=x-k[x];
        if(check(y)){
            vis[y]=true;
            q.push({y,t.step+1});
        }
        y=x+k[x];
        if(check(y)){
            vis[y]=true;
            q.push({y,t.step+1});
        }
    } 
    return -1; 
}
signed main(){
    cin>>n>>a>>b;
    for(int i=1;i<=n;i++){
        cin>>k[i];
    }
    cout<<bfs()<<endl;
    return 0;
}

P1588 [USACO07OPEN] Catch That Cow S

不是,这题和第一题一样啊!代码也一样,这是加了多组测试数据而已。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=4e5+6;
bool vis[maxn];
struct node{
    int x;
    int step;
};
int a,b;
int bfs(int x,int y){
    memset(vis,0,sizeof(vis));
    queue<node>q;
    q.push({x,0});
    vis[x]=true;
    while(!q.empty()){
        node t=q.front();
        q.pop();
        if(t.x==y){
            return t.step;
        }
        if(t.x-1>=1&&t.x-1<=maxn&&!vis[t.x-1]){
            vis[t.x-1]=true;
            q.push({t.x-1,t.step+1});
        }
        if(t.x+1>=1&&t.x+1<=maxn&&!vis[t.x+1]){
            vis[t.x+1]=true;
            q.push({t.x+1,t.step+1});
        }
        if(t.x*2>=1&&t.x*2<=maxn&&!vis[t.x*2]){
            vis[t.x*2]=true;
            q.push({t.x*2,t.step+1});
        }
    }
    return 0;
}
void work(){
    cin>>a>>b;
    cout<<bfs(a,b)<<"\n";
    return ;
}
signed main(){
    int T;
    cin>>T;
    while(T--){
        work();
    }
    return 0;
}