双向bfs

自从暑假学长讲过这个之后,现在才开始写第一道双向bfs题目。其实双向bfs很简单就是分别从起点和终点做两个bfs,以此大幅度减少搜索的状态数,从而解决一些普通单向bfs容易超时的问题。

q1从起点bfs的队列,q2从终点bfs的队列,vis1存从起点bfs的状态,vis2存从终点bfs的状态。
双向bfs的核心:
在q1的bfs中的一种状态有三种情况:1.vis1中不存在,在vis2中存在,说明这个状态可以达到终点。2.vis1种不存在,vis2中不存在,将这种状态push到q1。3.vis1中存在,说明已访问过这种状态直接continue。
在q2的bfs中的一种状态也有三种情况:1.vis2中不存在,在vis1中存在,说明这个状态可以达到终点。2.vis2种不存在,vis1中不存在,将这种状态push到q2。3.vis2中存在,说明已访问过这种状态直接continue。

例题:hdu1401
题意:在一个8*8的棋盘上有4个棋子。给你初始状态和目标状态,询问初始状态能否通过移动棋子达到目标状态。移动的规则:1.只能上下左右四个方向2.一个棋子的移动方向的第一个位置没有棋子,就直接移动到第一个位置3.一个棋子的移动方向的第一个位置有棋子,若第二个位置没有棋子,可以跳过第一位置,直接移动到第二个位置。

这道题目其实很简单,但是要把一种状态表示就需要hash。我是直接用一个int的值表示了状态,因为只有4个棋子,每个棋子有x,y,共8个属性,只要先排一下序,每种状态对应的值肯定是唯一的。可能是经验不足,写得很麻烦,头都快晕了。
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;

struct node
{
    int x,y;
    node(int x=0,int y=0):x(x),y(y){}
    bool operator <(const node &a)const
    {
        if(x==a.x)return y<a.y;
        return x<a.x;
    }
};

struct point
{
    node po[4];
    point(){}
    point(node a,node b,node c,node d)
    {
        this->po[0]=a;
        this->po[1]=b;
        this->po[2]=c;
        this->po[3]=d;
    }
};
int dir[4][2]={1,0,-1,0,0,1,0,-1};//四个方向
int hs(point t)//把四个点hash成int值
{
    sort(t.po,t.po+4);//先排序
    int ret=0;
    for(int i=0;i<4;i++)
    {
        ret*=10;
        ret+=t.po[i].x;
        ret*=10;
        ret+=t.po[i].y;
    }
    return ret;
}

point fhs(int sta)//把一个hash值反过来转成四个点
{
    point ret;
    for(int i=3;i>=0;i--)
    {
        ret.po[i].y=sta%10;
        sta/=10;
        ret.po[i].x=sta%10;
        sta/=10;
    }
    return ret;
}

bool judge(point sta,int x,int y,int xb)//判断对应位置有没有棋子
{
    for(int i=0;i<4;i++)
    {
        if(i==xb)continue;
        if(sta.po[i].x==x&&sta.po[i].y==y)return false;
    }
    return true;
}

map<int ,bool>mp1,mp2;
struct node2
{
    int sta,step;//本来是最多8步,双向中一个方向最多是4步
    node2(int sta=0,int step=0):sta(sta),step(step){}
};

bool bfs(point a,point b)
{
    queue<node2>q1,q2;
    int hs1=hs(a);
    mp1[hs1]=true;
    q1.push(node2(hs1,0));
    hs1=hs(b);
    mp2[hs1]=true;
    q2.push(node2(hs1,0));
    while(!q1.empty()||!q2.empty())
    {
        if(!q1.empty())
        {
            node2 now=q1.front();
            q1.pop();
            point tp=fhs(now.sta);//获得当前状态的四个点
            if(mp2[now.sta])return true;
            for(int i=0;i<4;i++)//枚举四个棋子,哪个棋子来移动
            {
                for(int j=0;j<4;j++)//枚举四个方向
                {
                    int dx=tp.po[i].x+dir[j][0];
                    int dy=tp.po[i].y+dir[j][1];
                    if(judge(tp,dx,dy,i))//相邻位置没有棋子
                    {
                        point nex=tp;
                        nex.po[i].x=dx;
                        nex.po[i].y=dy;
                        int hs2=hs(nex);
                        if(mp1[hs2]||now.step==4)continue;//如果已经在这个方向上访问过,或者下个状态的步数要大于4,剪枝
                        if(mp2[hs2])return true;//如果在反向上访问过,说明可以通过这个状态达到终点
                        mp1[hs2]=true;
                        q1.push(node2(hs2,now.step+1));
                    }
                    else//相邻的位置有棋子
                    {
                        dx+=dir[j][0];
                        dy+=dir[j][1];
                        if(judge(tp,dx,dy,i))//判断这个方向上的第二个位置
                        {
                            point nex=tp;
                            nex.po[i].x=dx;
                            nex.po[i].y=dy;
                            int hs2=hs(nex);
                            if(mp1[hs2]||now.step==4)continue;
                            if(mp2[hs2])return true;
                            mp1[hs2]=true;
                            q1.push(node2(hs2,now.step+1));
                        }
                    }
                }
            }
        }
        if(!q2.empty())
        {
            node2 now=q2.front();
            q2.pop();
            point tp=fhs(now.sta);
            if(mp1[now.sta])return true;
            for(int i=0;i<4;i++)
            {
                for(int j=0;j<4;j++)
                {
                    int dx=tp.po[i].x+dir[j][0];
                    int dy=tp.po[i].y+dir[j][1];
                    if(judge(tp,dx,dy,i))
                    {
                        point nex=tp;
                        nex.po[i].x=dx;
                        nex.po[i].y=dy;
                        sort(nex.po,nex.po+4);
                        int hs2=hs(nex);
                        if(mp2[hs2]||now.step==4)continue;
                        if(mp1[hs2])return true;
                        mp2[hs2]=true;
                        q2.push(node2(hs2,now.step+1));
                    }
                    else
                    {
                        dx+=dir[j][0];
                        dy+=dir[j][1];
                        if(judge(tp,dx,dy,i))
                        {
                            point nex=tp;
                            nex.po[i].x=dx;
                            nex.po[i].y=dy;
                            sort(nex.po,nex.po+4);
                            int hs2=hs(nex);
                            if(mp2[hs2]||now.step==4)continue;
                            if(mp1[hs2])return true;
                            mp2[hs2]=true;
                            q2.push(node2(hs2,now.step+1));
                        }
                    }
                }
            }
        }
    }
    return false;
}

int main()
{
    point sta,goal;
    while(scanf("%d%d",&sta.po[0].x,&sta.po[0].y)!=EOF)//多组输入
    {
        mp1.clear();
        mp2.clear();
        for(int i=1;i<4;i++)
            scanf("%d%d",&sta.po[i].x,&sta.po[i].y);
        for(int i=0;i<4;i++)
            scanf("%d%d",&goal.po[i].x,&goal.po[i].y);
        printf("%s\n",bfs(sta,goal)?"YES":"NO");
    }
    return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
八数码问题的解决方法中,C语言可以使用BFS(广度优先搜索)算法来解决。在C语言中,可以通过实现一个EightDigital类来解决八数码问题。通过BFS算法可以找到从初始状态到目标状态的最短路径。在该算法中,通过队列的方式,一层一层地扩展状态空间,直到找到目标状态。 在该实现中,可以使用一个辅助数组来记录每个状态的前驱状态,以便打印出从初始状态到目标状态的路径。具体实现中,可以使用一个stack来存储路径,从目标状态开始,通过查找前驱状态一直到初始状态,将每个状态压入栈中,最后依次弹出栈顶元素即可得到路径。同时,使用一个变量记录步数,即栈的大小,即可得到解的步数。 除了BFS算法外,还可以使用A*算法来解决八数码问题。A*算法是一种启发式搜索算法,通过评估函数来估计从当前状态到目标状态的代价,并选择代价最小的状态进行扩展。在八数码问题中,可以使用曼哈顿距离作为评估函数,即当前状态到目标状态的每个数字所需的水平和垂直移动的总和。通过A*算法可以更快地找到最优解。 总结起来,八数码问题可以使用C语言中的BFS算法和A*算法来解决。BFS算法可以找到最短路径,A*算法可以更快地找到最优解。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [c++八数码难题全家桶(A*算法、双向BFSBFS、DFS)](https://blog.csdn.net/qq_54893805/article/details/127440809)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值