hdu3567 Eight II (双向广搜+哈希)

Eight II

                                                                             Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)
                                                                                               Total Submission(s): 1562    Accepted Submission(s): 334


Problem Description
Eight-puzzle, which is also called "Nine grids", comes from an old game. 

In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.

We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.



A state of the board can be represented by a string S using the rule showed below.



The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.
 

Input
The first line is T (T <= 200), which means the number of test cases of this problem.
The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.

Output
For each test case two lines are expected.

The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.
 

Sample Input
 
   
2 12X453786 12345678X 564178X23 7568X4123
 

Sample Output
 
   
Case 1: 2 dd Case 2: 8 urrulldr
    思路:这题刚开始直接用了单向广搜,果断超时了。最后改用双向广搜。双向广搜的难点就在于字典序不好处理。正向的每次只要优先选择 字典序小的方向先进队列就可以了。逆向的要麻烦点,除了每次先往 字典序大的搜,当出现重复的序列(之前逆向搜到的)且先前搜到的该序列的步数与当前步数一致,应判断两串相同序列的移动字典序(判断字典序只需判断位移到该序列的最后一步操作的字典序,因为之前是按字典序大的先搜,所以先出现的序列,除最后一步操作以外的字典序一定是最大的),若当前的位移字典序列较大,则替换先前的序列。
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

queueq;
typedef struct
{
    char s[11];
    int x,y,num,cou,f;  //cou记录步数,f记录逆向最后一步的方向(用于判断字典序),num存父节点
}qq;
int axy[][2]={{1,0},{0,-1},{0,1},{-1,0}};//正向的方向
int axy2[][2]={{-1,0},{0,1},{0,-1},{1,0}};//逆向的方向
qq s[500000];     //存搜到的序列
int r,h[1000003],nex[1000003];
qq st[500000];
int sf;

int Hash(char s[])
{
    unsigned int hash=0,seed=1;
    for(int i=0;s[i];i++)
    {
        hash=hash+(s[i]-'0')*seed;
        seed*=10;
    }
    return  hash%1000003;
}

int Find(int k,char sc[])
{
    int u,n=Hash(sc);
    u=h[n];
    while(u)
    {
        if(strcmp(s[u].s,sc)==0)  //出现已搜过的序列,将该序列的存贮下标返回
            return u;
        u=nex[u];
    }
    nex[k]=h[n];   //用链表的形式存储新出现的序列
    h[n]=k;
    return 0;
}

void change(int i,int j,char ss[])  //X换位
{
    char c;
    c=ss[i];
    ss[i]=ss[j];
    ss[j]=c;
}
int f[500005];  //标记每个序列是逆向搜到的还是正向搜到的
qq pf;
int BFS(char s1[],char s2[],int x,int y,int x2,int y2)
{
    int k,i,u,cou,flag=99999999;
    char sc[20];
    r=1;
    memset(f,0,sizeof(f));
    memset(h,0,sizeof(h));
    memset(nex,0,sizeof(nex));
    while(!q.empty())q.pop();
    s[0].num=0;
    Find(r,s2);

    strcpy(s[r].s,s2);
    s[r].cou=0;
    s[r].x=x2;
    s[r].y=y2;
    s[r].num=0;
    f[r]=2;
    s[r].f=0;
    q.push(r);
    r++;

    Find(r,s1);
    strcpy(s[r].s,s1);
    s[r].cou=0;
    s[r].x=x;
    s[r].y=y;
    s[r].num=0;
    f[r]=1;
    s[r].f=0;
    q.push(r);
    r++;
//printf("--%s  %s\n",s1,s2);
    while(!q.empty())
    {
        k=q.front();
        q.pop();
        if(s[k].cou>flag)return 0;

       cou=s[k].cou;
       if(f[k]==1) //正向搜
        for(i=0;i<4;i++)
        {
            x=s[k].x+axy[i][0];
            y=s[k].y+axy[i][1];
            strcpy(sc,s[k].s);
            if(x<3&&y<3&&x>=0&&y>=0)
            {
                change(x*3+y,s[k].x*3+s[k].y,sc); //换位
                u=Find(r,sc);   //判重
                s[r].x=x;
                s[r].y=y;
                s[r].num=k;
                s[r].cou=cou+1;
                s[r].f=i;
                if(u) //有重复
                {
                    pf.x=r,pf.y=u;
                    pf.cou=s[r].cou+s[u].cou;
                    if(f[k]!=f[u]) //正逆向搜到同一个序列
                    {
                        //if(flag>s[k].cou)
                        flag=s[k].cou;      //标记搜到正逆向对碰时的层数

                        st[sf++]=pf;        //将同一层存起来
                        strcpy(s[r].s,sc);
                        r++;
                    }
                    continue;
                }
                strcpy(s[r].s,sc);
                q.push(r);
                f[r]=f[k];
                r++;
            }
        }
        if(f[k]==2) //逆向搜
        for(i=0;i<4;i++)
        {
            x=s[k].x+axy2[i][0];
            y=s[k].y+axy2[i][1];
            strcpy(sc,s[k].s);
            if(x<3&&y<3&&x>=0&&y>=0)
            {
                change(x*3+y,s[k].x*3+s[k].y,sc);
                u=Find(r,sc); //判重
                s[r].x=x;
                s[r].y=y;
                s[r].num=k;
                s[r].cou=cou+1;
                s[r].f=i;
                if(u)
                {
                    pf.x=u,pf.y=r;
                    pf.cou=s[r].cou+s[u].cou;
                    if(f[k]!=f[u])  //若出现
                    {
                        if(flag>s[k].cou)
                        flag=s[k].cou;
                        st[sf++]=pf;
                        strcpy(s[r].s,sc);
                        r++;
                    }
                    else if(f[k]==2&&f[u]==2) //如果搜到前面已出现的序列(逆向搜到)

                    {
                        if(s[u].f>i&&s[k].cou+1==s[u].cou) //若字典序大于先前搜到的字典序,更新父节点
                        {
                            s[u].num=k;
                            s[u].f=i;
                        }
                    }
                    continue;
                }
                strcpy(s[r].s,sc);
                q.push(r);
                f[r]=f[k];
                r++;
            }
        }
    }
    pf.y=pf.x=0;
    return 0;
}

string ans[50000];
int af;

void solve()
{
    int i,u,k,l=0,p[2000];

    for(i=0;is[u].x)
                p[l++]='u';
            else if(s[k].ys[u].y)
                p[l++]='l';
            u=k;
        }
        if(l>0)
        {   //printf("\n--\n");
            p[l]='\0';
            for(int j=l-1;j>=0;j--)
            {
                //printf("%c",p[j]);
                ans[i]+=p[j];
            }

        }
        u=st[i].y;
        while(s[u].num)
        {
            k=s[u].num;

            if(s[k].xs[u].x)
                {ans[i]+='d';}
            else if(s[k].ys[u].y)
                {ans[i]+='r';}
            u=k;
        }
    }
    sort(ans,ans+sf);  //按字典序排序
    printf("%d\n",st[0].cou);
    //for(i=0;i


 
  

你可能感兴趣的:(搜索)