广度优先搜索BFS

广度优先搜索

一、使用情况

1.寻找最短,最少(目前感觉这个比较多哦)

2.地毯式搜索

二、模板

void  bfs(){

   q.push(start);

while(!q.empty()){

node temp=q.front();

q.pop();

visit(temp)=1;

for(int i=0;i<n;i++)

if(符合条件)q.push();

}

三、落谷总结

1.字串变换

字符串的替换:str.replace(开始位置,要替换的字串长度,目标替换字串);

子串的查找:str.find(查找子串);

注意:在匹配子串进行替换的时候,要匹配完。

代码:#include<iostream>
#include<queue>
#include<map>
#include<string>
using namespace std;
struct node{
    string s;
    int num;    
};
string sa,sb,s1[10],s2[10];
map<string,int> visit;
queue<node> point;
int n=0;
void init(){
    cin>>sa>>sb;
    while(cin>>s1[n]>>s2[n])n++;
    n--;
}
void bfs(){
    node start;
    start.s=sa;
    start.num=0;
    point.push(start);
    while(!point.empty()){
        node  temp=point.front();
        point.pop();
        if(temp.s==sb&&temp.num<11){
            cout<<temp.num <<endl;
            return;
        }
        if(visit[temp.s]==0){
            visit[temp.s]=1;
            for(int i=0;i<=n;i++){
                if(temp.s.find(s1[i])>=0) {
                    for(int j=temp.s.find(s1[i]);j>=0&&j<temp.s.size();j=temp.s.find(s1[i],j+1)){
                        node temp2=temp;
                        temp2.num++;
                        temp2.s.replace(j,s1[i].size(),s2[i]);
                        point.push(temp2);
                    }
                }
            }
        }
        
    }
    cout<<"NO ANSWER!"<<endl;
}
int main(){
    init();
    bfs();
    return 0;
}

2.机器人搬重物:

主要是模拟和方向的处理,要敢写丫!

另外,换方向也是一步。每个节点的四个方向都要 单独标记!

代码:#include<iostream>
#include<queue>
using namespace std;
int n,m;
int a[55][55];
int der[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int vis[55][55][4];
int sx,sy,ex,ey;
char pos;
struct node{
    int x,y;
    int vac;
    int step;
};
queue<node> q;
bool check(int x,int y){
    if(x<1||x>=n||y<1||y>=m||a[x][y]||a[x+1][y]||a[x][y+1]||a[x+1][y+1])return false;
    return true;
}
int bfs(node start){
    q.push(start);
    while(!q.empty()){
        node temp=q.front() ;
        q.pop();
        if(temp.x ==ex&&temp.y ==ey){
            return temp.step ;
        }
        if(vis[temp.x][temp.y][temp.vac ])continue;
        vis[temp.x][temp.y][temp.vac ]=1;
        node temp1=temp,temp2=temp;
        temp1.step =temp2.step =temp.step +1;
        if(temp.vac ==0){
            temp1.vac =3;
            temp2.vac =2;
        }
        if(temp.vac ==1){
            temp1.vac =2;
            temp2.vac =3;
        }
        if(temp.vac ==2){
            temp1.vac =0;
            temp2.vac =1;
        }
        if(temp.vac ==3){
            temp1.vac =1;
            temp2.vac =0;
        }
        q.push(temp1);
        q.push(temp2);
         for(int i=1;i<=3;i++){
             int tx=temp.x+der[temp.vac ][0]*i,ty=temp.y+der[temp.vac][1]*i;
             if(!check(tx,ty))break;
             node temp3=temp;
             temp3.x =tx;
             temp3.y =ty;
             temp3.step =temp.step +1;
             q.push(temp3);
         }
    }
    return -1;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)
        cin>>a[i][j];    
    }
    cin>>sx>>sy>>ex>>ey>>pos;
    node start;
    start.x =sx;
    start.y=sy;
    switch(pos){
        case 'N':{
            start.vac =0;
            break;
        };
        case 'S':{
            start.vac =1;
            break;
        };
        case 'E':{
            start.vac =2;
            break;
        };
        case 'W':{
            start.vac =3;
            break;
        }
    }
    start.step =0;
    cout<<bfs(start)<<endl;
}

原文地址:https://www.cnblogs.com/wtx2333/p/11715588.html