P1379 八数码难题(双向bfs)

题目传送门

题意: 在一个九宫格内,有8个数和一个0,每次可以将0与和他相邻的数交换位置,给定初始串,输入目标串,要求输出最少操作次数。

双向bfs: 双向bfs是在我们知道起点和终点的情况下,同时由起点和终点出发,展开bfs,一旦遇到对方走过的点,就找到了最短路径。单向的bfs,它要求在访问完当前层的节点,才会去访问下一层的节点,当我们起点到终点路径比较长的时候,每一层的节点数都可能很大,我们就访问了很多不必要的节点,但是如果我们双向遍历,我们两个队列访问到的层数均摊,这样就避免了访问那么多无效节点,从而将时间大大降低。

单向bfs时间:

双向bfs时间:

双向bfs代码:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=1e3+5;
const int inf=0x7fffffff;
const int mod=998244353;
const double eps=1e-6;
map<string,int>ma;
int finds(string s)
{
    for(int i=0;i<s.size();i++)
        if(s[i]=='0')
            return i;
}
struct node
{
    string t;
    int st;
};
int moves(int p,queue<node>&q,node tt)
{
    node h;
    if((p+1)%3==p%3+1&&(p+1)>=0&&(p+1)<=8)
    {
        h=tt;
        swap(h.t[p],h.t[p+1]);
        if(ma[h.t]*ma[tt.t]<0)
            return abs(ma[h.t])+abs(ma[tt.t])-1;
        if(h.st>0)
            h.st++;
        else
            h.st--;
        if(!ma[h.t])
        {
            q.push(h);
            ma[h.t]=h.st;
        }
    }
    if((p-1)%3==p%3-1&&(p-1)>=0&&(p-1)<=8)
    {
        h=tt;
        swap(h.t[p],h.t[p-1]);
        if(ma[h.t]*ma[tt.t]<0)
            return abs(ma[h.t])+abs(ma[tt.t])-1;
        if(h.st>0)
            h.st++;
        else
            h.st--;
        if(!ma[h.t])
        {
            q.push(h);
            ma[h.t]=h.st;
        }
    }
    if(p-3>=0&&(p-3)<=8)
    {
        h=tt;
        swap(h.t[p],h.t[p-3]);
        if(ma[h.t]*ma[tt.t]<0)
            return abs(ma[h.t])+abs(ma[tt.t])-1;
        if(h.st>0)
            h.st++;
        else
            h.st--;
        if(!ma[h.t])
        {
            q.push(h);
            ma[h.t]=h.st;
        }
    }
    if(p+3<=8&&(p+3)>=0)
    {
        h=tt;
        swap(h.t[p],h.t[p+3]);
        if(ma[h.t]*ma[tt.t]<0)
            return abs(ma[h.t])+abs(ma[tt.t])-1;
        if(h.st>0)
            h.st++;
        else
            h.st--;
        if(!ma[h.t])
        {
            q.push(h);
            ma[h.t]=h.st;
        }
    }
    return -1;
}
int bfs(string en)
{
    string sta="123804765";
    queue<node>q1,q2;
    ma.clear();
    node a;a.t=sta;a.st=1;
    node b;b.t=en;b.st=-1;
    q1.push(a);q2.push(b);
    ma[sta]=1;ma[en]=-1;
    while(!q1.empty()&&!q2.empty())
    {
        a=q1.front();b=q2.front();
        q1.pop();q2.pop();
        int p1=finds(a.t),p2=finds(b.t);
        int mov1=moves(p1,q1,a);
        if(mov1!=-1)
            return mov1;
        int mov2=moves(p2,q2,b);
        if(mov2!=-1)
            return mov2;
    }
}
signed main()
{
    string s;
    cin>>s;
    if(s=="123804765")
        cout<<0<<endl;
    else
    cout<<bfs(s)<<endl;
}

单向bfs代码:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=1e6+5;
const int inf=0x7fffffff;
const int mod=998244353;
const double eps=1e-6;
map<string,int>ma;
struct node
{
    string s;int t;
};
int finds(string s)
{
    for(int i=0;i<9;i++)
    {
        if(s[i]=='0')
            return i;
    }
}
int moves(node a,queue<node>&q,int pos,string en)
{
    if(a.s==en)
    {
        return a.t-1;
    }
    node b;
    if((pos-1)%3==pos%3-1&&pos-1>=0)
    {
        b=a;
        swap(b.s[pos],b.s[pos-1]);
        b.t++;
        if(b.s==en)
        {
            return b.t-1;
        }
        if(!ma[b.s])
        {
            ma[b.s]=b.t;
            q.push(b);
        }
    }
    if((pos+1)%3==pos%3+1&&pos+1<=8)
    {
        b=a;
        swap(b.s[pos],b.s[pos+1]);
        b.t++;
        if(b.s==en)
        {
            return b.t-1;
        }
        if(!ma[b.s])
        {
            ma[b.s]=b.t;
            q.push(b);
        }
    }
    if(pos+3<=8)
    {
        b=a;
        swap(b.s[pos],b.s[pos+3]);
        b.t++;
        if(b.s==en)
            return b.t-1;
        if(!ma[b.s])
        {
            ma[b.s]=b.t;
            q.push(b);
        }
    }
    if(pos-3>=0)
    {
        b=a;
        swap(b.s[pos],b.s[pos-3]);
        b.t++;
        if(b.s==en)
            return b.t-1;
        if(!ma[b.s])
        {
            ma[b.s]=b.t;
            q.push(b);
        }
    }
    return -1;
}
int bfs(string en)
{
    string sta="123804765";
    queue<node>q;
    node a={sta,1};
    q.push(a);
    while(!q.empty())
    {
        a=q.front();q.pop();
        int pos=finds(a.s);
        int res=moves(a,q,pos,en);
        if(res!=-1)
            return res;
    }
}
signed main()
{
    string en;
    cin>>en;
    cout<<bfs(en)<<endl;
}

原创文章 144 获赞 13 访问量 8656

猜你喜欢

转载自blog.csdn.net/Joker_He/article/details/106130264