【双向BFS搜索】hdu3567Eight II

http://acm.hdu.edu.cn/showproblem.php?pid=3567
题目大意:8数码问题。给定起始状态和目标状态,求最少移动次数和字典序最小的方案。(r:向右,l:向左,u:向上,d:向下)

用单向BFS–>无限TLE
可以用双向BFS和 A 搜索,但注意由于要求字典序最小,无论是双向BFS,还是 A 搜索都无法保证最先出现的解字典序最小。无奈只得将最小移动次数全都搜索一次。据说双向BFS比 A 搜索快,蒟蒻就写的双向BFS。

搜索过程中,若用字符串来记录路径很显然占用空间太大,会MLE的……采用4进制数来保存路径。正向的BFS很好办,逆向的肿么办?
正向: path=oldpath4+i (0<=i<=3)
逆向: path=(3i)4step+oldpath(0<=i<=3) 其中step表示当前移动次数

很经典的一道搜索题。然而蒟蒻调了半天才过……弱爆了……
蒟蒻加油 ↖(^ω^)↗

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define LL long long int
#define MAXN 363880
using namespace std;

int power[15]={1,1,2,6,24,120,720,5040,40320,362880,3628800};
char begi[15] ,en[15] ;
int dir[4]={3,-1,1,-3} ,pos_b ,pos_e ;
char word[5]="dlru" ;
int visit[MAXN+10][2] ;
LL road[MAXN+10][2] ,p2[55] ;

struct node
{
    int step ,k ,pos_0 ;
    LL way ;
    char temp[15];
    node(){}
    node(const char a[],const int b,const LL c,const int d,const int e)
    {
        memset(temp,0,sizeof temp);
        strcpy(temp,a);
        step=b ,way=c ,k=d ,pos_0=e ;
    }
}n ;

int Hash(char a[])
{
    int ans=0 ,tmp ;
    for(int i=0;i<9;i++)
    {
        tmp=0;
        for(int j=i+1;j<9;j++)
            if(a[i]>a[j])
                tmp++;
        ans+=tmp*power[8-i];
    }
    return ans;
}

string get_way(LL a,int b)
{
    int str[100] ,cnt=0 ;
    for(int i=1;i<=b;++i)
    {
        str[++cnt]=a%4;
        a/=4;
    }
    string ans="";
    for(int i=cnt;i>0;--i)
        ans+=word[str[i]];
    return ans;
}

void bfs()
{
    if(strcmp(begi,en)==0)
    {
        printf("0\n\n");
        return;
    }
    memset(visit,-1,sizeof visit);
    memset(road,0,sizeof road);

    queue<node>point;
    point.push(node(begi,0,0,0,pos_b));
    visit[Hash(begi)][0]=0;
    point.push(node(en,0,0,1,pos_e));
    visit[Hash(en)][1]=0;

    char temp[15] ;
    int pos_0 ,lin ,pos ,ans=1<<30;
    LL tmp ;
    string anspath="";
    while(!point.empty())
    {
        n=point.front();
        pos_0=n.pos_0 ;
        strcpy(temp,n.temp);
        point.pop();

        for(int i=0;i<4;++i)
        {
            pos=pos_0+dir[i];
            if(pos>-1&&pos<9&&(pos/3==pos_0/3||pos%3==pos_0%3))
            {
                swap(temp[pos_0],temp[pos]);
                lin=Hash(temp);

                if(visit[lin][n.k]!=-1)
                {
                    if(n.step+1>visit[lin][n.k])
                    {
                        swap(temp[pos_0],temp[pos]);
                        continue;
                    }
                    if(n.k)
                        tmp=(3-i)*p2[n.step]+n.way;
                    else tmp=n.way*4+i;
                    road[lin][n.k]=min(road[lin][n.k],tmp);
                }
                else
                {
                    visit[lin][n.k]=n.step+1;
                    if(n.k)
                        road[lin][1]=(3-i)*p2[n.step]+n.way;
                    else road[lin][0]=n.way*4+i;
                }

                if(visit[lin][!n.k]!=-1)
                {
                    string path=get_way(road[lin][0],visit[lin][0])+get_way(road[lin][1],visit[lin][1]);
                    int len=path.size();
                    if(len>ans)
                    {
                        printf("%d\n",ans);
                        cout<<anspath<<endl;
                        return;
                    }
                    else if(ans>len)
                    {
                        ans=len;
                        anspath=path;
                    }
                    else if(anspath>path)
                        anspath=path;
                }
                point.push(node(temp,n.step+1,road[lin][n.k],n.k,pos));
                swap(temp[pos_0],temp[pos]);
            }
        }
    }
}

int main()
{
    p2[0]=1;
    for(int i=1;i<=28;++i)
        p2[i]=p2[i-1]*4;
    int T ;
    scanf("%d",&T);
    for(int cas=1;cas<=T;++cas)
    {
        scanf("%s%s",begi,en);
        for(int i=0;i<9;++i)
        {
            if(begi[i]=='X')
                pos_b=i,begi[i]='0';
            if(en[i]=='X')
                pos_e=i,en[i]='0';
        }
        printf("Case %d: ",cas);
        bfs();
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值