hdu 3567 Eight II (bfs+康托展开+预处理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567

Eight II

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


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
 

Author
zhymaoiing
 

Source
 

Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:   3459  2918  1043  1401  2691 

题目大意:求由字符串A转换成字符串B所需的最少步数。
注意一下几点:
1、输出满足条件中字典树最小的输出。
2、采用预处理的方式将可能出现的九种情况全部列出来。
3、利用康托展开将排列转换成唯一对应的数字。
4、注意还需输出Case以及步数。
5、代码非常容易超时,以下提供的代码请选择G++进行提交。

详见代码。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <string>

using namespace std;

int dir[4][2]= {1,0,0,-1,0,1,-1,0};//分别对应右,左,下。上四个方向
char dd[5]= {"dlru"};
int num[15],vis[500000];
int pre[15][500000];
char ans[15][500000];//pre表示先前的变换方式,ans表示现有的变换

struct node
{
    int x;
    int y;
   // string step;//用来记录变化的过程
    int val;//表示当前状态的康拓值
    int ns[10][10];//表示当前字符串处于什么状态。采用矩阵的方法表示
    /*void change(string &str)//将矩阵转换成字符串,因为康托展开是对字符串进行处理
    {
        str="";
        for (int i=0; i<3; i++)
        {
            for (int j=0; j<3; j++)
            {
                str+=ns[i][j];//将字符串直接一个一个加到str上,十七变成字符串
            }
        }
    }*/
};

void fun()//求阶乘的函数
{
    num[0]=1;
    for (int i=1; i<=10; i++)
    {
        num[i]=num[i-1]*i;
    }
}

void fun1(int k,int Cantor,int d)//按照对应关系转换回去在输出
{
    char fd[100];
    int len=0;
    while(pre[k][Cantor]!=-1)
    {
        fd[len++]=ans[k][Cantor];
        Cantor=pre[k][Cantor];
    }
    printf("%d\n",len);
    for(int i=len-1; i>=0; i--)
        printf("%c",fd[i]);
    /*if(ans[k][Cantor]==-1)
    {
        printf("%d\n",d);
        return ;
    }
    fun(k,ans[k][Cantor],d+1);
    printf("%c",an[k][Cantor]);*/
}

int Cantor(node &s)
{
    int ans=0;
    for (int i=0; i<8; i++)
    {
        int t=0;
        for (int j=i+1; j<9; j++)
        {
            if (s.ns[i/3][i%3]>s.ns[j/3][j%3])
                t++;
        }
        ans+=t*num[9-i-1];
    }
    return ans;
}

void bfs(string &str,int tt)
{
    node s,ss;
    memset(vis,0,sizeof(vis));
    queue<node>q;
    int k=0;
    for (int i=0; i<3; i++)
    {
        for (int j=0; j<3; j++)
        {
            s.ns[i][j]=str[k++];//将字符串变为矩阵
            if (s.ns[i][j]=='9')
            {
                s.x=i;
                s.y=j;
            }
        }
    }
    //s.step="";
    //s.change(str);
    int r=Cantor(s);
    vis[r]=1;
    s.val=r;
    pre[tt][r]=-1;//初始没有前驱
    q.push(s);
    while (!q.empty())
    {
        s=q.front();
        q.pop();
        for (int i=0; i<4; i++)
        {
            int X=s.x+dir[i][0];
            int Y=s.y+dir[i][1];
            if (X>=0&&X<3&&Y>=0&&Y<3)
            {
                ss=s;
                ss.x=X;
                ss.y=Y;
                char t;
                t=ss.ns[s.x][s.y];
                ss.ns[s.x][s.y]=ss.ns[X][Y];
                ss.ns[X][Y]=t;
                //swap(ss.ns[s.x][s.y],ss.ns[X][Y]);
                //ss.change(str);
                r=Cantor(ss);
                ss.val=r;
                if (!vis[r])
                {
                    pre[tt][r]=s.val;//当前康拓值的前一状态,将ans连接在一起
                    ans[tt][r]=dd[i];//表示这步状态是用哪一种方式变过来的
                    vis[r]=1;
                    q.push(ss);
                }
            }
        }
    }
}

int main()
{
    int t;
    int flag=1;
    string str,ch;
    //memset(vis,0,sizeof(vis));
    fun();
    str="912345678";
    bfs(str,0);
    str="192345678";
    bfs(str,1);
    str="129345678";
    bfs(str,2);
    str="123945678";
    bfs(str,3);
    str="123495678";
    bfs(str,4);
    str="123459678";
    bfs(str,5);
    str="123456978";
    bfs(str,6);
    str="123456798";
    bfs(str,7);
    str="123456789";
    bfs(str,8);
    scanf("%d",&t);
    while (t--)
    {
        cin>>str>>ch;
        printf ("Case %d: ",flag++);
        //bfs(str,ch);
        int xx[15],sx,k=1;
        for (int i=0; i<9; i++)//准备用来预处理的对应关系
        {
            if (str[i]=='X')
            {
                sx=i;//sx表示X所在的位置
                continue;
            }
            xx[str[i]-'0']=k++;
        }
        for (int i=0; i<9; i++)//按照对应关系转换回去在输出
        {
            if (ch[i]=='X')
            {
                ch[i]='9';
                continue;
            }
            ch[i]=xx[ch[i]-'0']+'0';
        }
        node s;
        int kk=0;
        for (int i=0; i<3; i++)
        {
            for (int j=0; j<3; j++)
            {
                s.ns[i][j]=ch[kk++]-'0';//将字符串变为矩阵
            }
        }
        int qq=Cantor(s);
        //cout<<ch<<endl;
        fun1(sx,qq,0);
        printf ("\n");
    }
    return 0;
}



 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值