自从暑假学长讲过这个之后,现在才开始写第一道双向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;
}