HDU 3567

22 篇文章 0 订阅

Eight II

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 130000/65536K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 3
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
2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC
 

和之前做的八数码差不多

因为X的位置不同  多出了9倍的状态  所以才去保存路径的方式预处理答案

因为题目保证都是合法情况  不需要判断逆序数

然后和八数码差不多思路  不需要A*  直接广搜预处理即可


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<string>
#define inf 1<<30
#define eps 1e-7
#define LD long double
#define LL long long
#define maxn 1000000005
using namespace std;
int ed;
int fac[3][3]= {{1,1,2},{6,24,120},{720,5040,40320}};//计算康拓值
struct node
{
    int Map[3][3];
    int x,y;
    int Hash;
    bool cheak()
    {
        if(x>=0&&x<3&&y>=0&&y<3)
            return true ;
        return false ;
    }
} u,uu,v;
int vis[9][400000];//标记
int pri[9][400000];//路径
int dir[4][2]= {{1,0},{0,-1},{0,1},{-1,0}};

int get_hash(int maze[3][3])  //计算康拓值
{
    int ret=0;
    for(int i=0; i<3; i++)
        for(int j=0; j<3; j++)
        {
            int cnt=0;
            for(int a=0; a<i; a++)
                for(int b=0; b<3; b++)
                    if(maze[a][b]>maze[i][j])
                        cnt++;
            for(int b=0; b<j; b++)
                if(maze[i][b]>maze[i][j])
                    cnt++;
            ret+=cnt*fac[i][j];
        }
    return ret;
}

char ch[5]= {"dlru"};
//输出路径
void print(int i)
{
    int st=ed;

    int cnt=0,ans[1000];

    while(pri[i][st]!=-2)
    {
        ans[cnt++]=vis[i][st];
        st=pri[i][st];
    }
    printf("%d\n",cnt);
    for(int i=cnt-1; i>=0; i--)
        printf("%c",ch[ans[i]]);
    printf("\n");
    return ;
}

void bfs(int kind)
{
    queue<node>que;
    que.push(u);
    pri[kind][u.Hash]=-2;
    while(!que.empty())
    {
        uu=que.front();
        que.pop();
        for(int i=0; i<4; i++)
        {
            v=uu;
            v.x+=dir[i][0];
            v.y+=dir[i][1];
            if(v.cheak())
            {
                swap(v.Map[v.x][v.y],v.Map[uu.x][uu.y]);
                v.Hash=get_hash(v.Map);
                if(pri[kind][v.Hash]==-1)
                {
                    pri[kind][v.Hash]=uu.Hash;
                    vis[kind][v.Hash]=i;
                    que.push(v);
                }
            }
        }
    }
}

void Init(char *str,int kind)
{
    int k=0;
    for(int i=0; i<3; i++)
        for(int j=0; j<3; j++)
        {
            if(str[k]=='X')
                u.Map[i][j]=0;
            else
                u.Map[i][j]=str[k]-'0';
            k++;
        }
    u.x=kind/3;
    u.y=kind%3;
    u.Hash=get_hash(u.Map);
    bfs(kind);
}

int main()
{
    int t;
    int tt=1;
    //预处理
    memset(pri,-1,sizeof(pri));
    Init("X12345678",0);
    Init("1X2345678",1);
    Init("12X345678",2);
    Init("123X45678",3);
    Init("1234X5678",4);
    Init("12345X678",5);
    Init("123456X78",6);
    Init("1234567X8",7);
    Init("12345678X",8);
    scanf("%d",&t);
    while(t--)
    {
        char s1[10],s2[10];
        scanf("%s%s",s1,s2);
        int a[3][3],b[3][3],pos,c[9],k=1;
        for(int i=0; i<9; i++)
        {
            if(s1[i]=='X')
            {
                c[0]=0;
                pos=i;
            }
            else
                c[s1[i]-'0']=k++;
        }
        for(int i=0; i<9; i++)
            b[i/3][i%3]=(s2[i]=='X'?c[0]:c[s2[i]-'0']);
        ed=get_hash(b);
        printf("Case %d: ",tt++);
        print(pos);
    }
    return 0;
}



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值