洛谷P1379——双向BFS

单向BFS
注意这里的stoi不是标准函数,有些OJ不认,慎用。
而C++str字符串可以直接用==来判断是否相等

woc,笔者脑子坏了,竟然用一维数组代替二维数组,也就是说
(一下按照程序员计数法)
加入此时0在第1行的第0个,那么0向左移动一位,就会跳到第0行的第2个,这时索引值是在正常范围内的,但是在真实世界中又是不可以的,这里千万注意。
注意啊,这个是错误示范

#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
struct node
{
	string s;
	int dis;	
    node(){}
	node(string s,int dis=0):s(s),dis(dis){}
};

queue<node>q;
map<string,bool>maap;
int dir[4]={-3,3,1,-1};


int BFS();

int main()
{
	string str,t="123804765";
	cin >>str;
	if(stoi(str)==stoi(t)){
		cout << 0 << endl;
	}else{
		q.push(node(str,0));
		cout << BFS() << endl;
	}
    system("pause");
	return 0;
}


int BFS()
{
	while(!q.empty()){
		node de,no=q.front();
		q.pop();
		if(no.s=="123804765"){
			return no.dis;
		}
		no.dis++;
        int j;
		for(j=0;j<10;j++){//找到0的坐标 
				if(no.s[j]=='0'){
					break;
				}
		}
		for(int i=0;i<4;i++){
			de=no;
			int k=j+dir[i];
			if(k<0||k>8){
				continue;
			}
			char temp=de.s[k];//0和其他数字进行交换 
			de.s[k]=de.s[j];
			de.s[j]=temp;
			//map判重
			if(!maap[de.s]){
				maap[de.s]=true;
				q.push(de);
			}
		}
	}
	return -1;
}

单向BFS正确示范

#include<iostream>
#include<queue>
#include<map>
#include<string>
using namespace std;

struct node
{
    string s;
    int dis;
    node(){}
    node(string s,int dis):s(s),dis(dis){}
};

int dir[4][2]={0,1,-1,0,0,-1,1,0};
queue<node>q;

int BFS();

int main()
{
    string s;
    cin >> s;
    q.push(node(s,0));
    cout << BFS() << endl;
    system("pause");
    return 0;
}

int BFS()
{
    map<string,bool>chessbord;
    while(!q.empty()){
        node no,de;
        no=q.front();
        q.pop();
        if(no.s=="123804765"){
            return no.dis;
        }
        no.dis++;
        char chess[3][3];
        int x,y;//用于储存0的坐标
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                chess[i][j]=no.s[i*3+j];
                if(chess[i][j]=='0'){
                    x=i;
                    y=j;
                }
            }
        }
        for(int i=0;i<4;i++){
            int x1=x+dir[i][0];
            int y1=y+dir[i][1];
            if(x1<0||x1>2||y1<0||y1>2){//判读是否在棋盘上
                continue;
            }
            //新坐标在棋盘上就进行交换
            char temp=chess[x][y];
            chess[x][y]=chess[x1][y1];
            chess[x1][y1]=temp;
            de=no;
            for(int j=0;j<3;j++){
                for(int k=0;k<3;k++){
                    de.s[3*j+k]=chess[j][k];
                }
            }
            //放入map判重
            if(!chessbord[de.s]){
                chessbord[de.s]=true;
                q.push(de);
            }
            temp=chess[x][y];
            chess[x][y]=chess[x1][y1];
            chess[x1][y1]=temp;
        }
    }
    return -1;
}

附上洛谷AC图片,注意看通过测试的时间,一会儿要和双向BFS作比较。
在这里插入图片描述

一下是双向BFS代码

#include<iostream>
#include<string>
#include<map>
#include<queue>
using namespace std;

string s,t="123804765";//储存初始情况和期望情况
int dir[4][2]={-1,0,1,0,0,-1,0,1};//0移动的四种可能

int D_BFS();//函数申明

int main()
{
    cin >> s;//读入初始状态
    if(s==t){//初上状态==期望状态
        cout << 0 << endl;
    }else{
        cout << D_BFS() << endl;
    }
    // system("pause");
    return 0;
}


int D_BFS()
{
    queue<string>p,q;//正向队列,反向队列
    p.push(s);//放入初始状态
    q.push(t);
    map<string,int>chessbord;//判重+标记
    map<string,int>dis;//存放走到当前状态步数
    bool flag;//标记当前循环方向
    chessbord[s]=1,chessbord[t]=2;//所有通过正向循环得到的情况在map中都是1,逆向循环得到的情况都是2
    dis[s]=0,dis[t]=0;//两个初始情况的步数都是0
    string no,de;
    //这里的BFS并不是一层层向外扩展的,而是假设从正向开始,从正向里面选取一个子节点,把这个子节点扩散开来,符合条件的保存到正向队列中,然后再进行判断,看哪个队列比较短就扩展哪一个,这样更加节省时间~
    while(!p.empty()||!q.empty()){//开始BFS循环
        if(p.size()<q.size()||(q.size()>q.size())&&q.empty()){//当队列Q1的长度小于队列Q2的长度或队列Q1的长度大于队列Q2的长度且队列Q2为空的时候
            no=p.front();
            p.pop();
            flag=true;
        }else{
            no=q.front();
            q.pop();
            flag=false;
        }
        
        char chess[3][3];
        int x,y;//用于储存0的坐标
        for(int i=0;i<3;i++){//把现在的情况铺到棋盘上
            for(int j=0;j<3;j++){
                chess[i][j]=no[i*3+j];
                if(chess[i][j]=='0'){//确定'0'所在的位置
                    x=i;
                    y=j;
                }
            }
        }
        for(int i=0;i<4;i++){//标准的BFS
            int x1=x+dir[i][0];
            int y1=y+dir[i][1];
            if(x1<0||x1>2||y1<0||y1>2){//判读是否在棋盘上
                continue;
            }
            //新坐标在棋盘上就进行交换
            char temp=chess[x][y];
            chess[x][y]=chess[x1][y1];
            chess[x1][y1]=temp;
            de=no;//de用于存放新棋盘上的字符串
            for(int j=0;j<3;j++){
                for(int k=0;k<3;k++){
                    de[3*j+k]=chess[j][k];
                }
            }
            //判断完之后再复原,为下一次循环做准备
            temp=chess[x][y];
            chess[x][y]=chess[x1][y1];
            chess[x1][y1]=temp;

            if(chessbord[de]==chessbord[no]){//和变化以前的那个字符串作比较,如果再map中得到的值是一样的,那么说明在这个方向的队列中重复出现了
                continue;
            }//这种是重复的情况,剩下两种情况分别是:找到了和新出现的值
            if(chessbord[de]+chessbord[no]==3){//如果当前值在map里面已经被另一方向标记了,那么我们用当前方向的方向值+另一方向的方向值和3作比较,相等则表示找到了
                return dis[de]+dis[no]+1;//这里为什么要加1?因为从no走到de的这一步还要被加上去
            }
            //剩下一种情况就是当前值为新情况,我们要把它放到当前方向的队列里去
            chessbord[de]=chessbord[no];//在map中给新产生的情况赋上方向值
            dis[de]=dis[no]+1;
            if(flag){//把新情况放入到对应的队列中
                p.push(de);
            }else{
                q.push(de);
            }
        }
    }
    return -1;
}

在这里插入图片描述
这是AC之后的信息,我们可以发下其效率大大提升了!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值