HDU 3567 Eight II 八数码(2)

B - B

  • 题目描述
    • INPUT
    • OUTPUT
    • Sample Input
    • Sample Output

题目描述

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

预处理:这里你需要知道其实每个串无非就9种情况,只与X的位置有关,其余的你可以直接做一个映射关系。
“X12345678” , “1X2345678” , “12X345678” , “123X45678” , “1234X5678” , “12345X678” , “123456X78” , “1234567X8” , “12345678X”
比如说示例1中的
12X453786
我们就可以做这样的一个映射“12X345678”
分别是:1 → 1,2 → 2,X → X,4 → 3,5 → 4,3 → 5,7 → 6,8 → 7,6 → 8,
这样我们只需要搜索8种基本状态记录路径就可以了。
然后目标状态一样要映射掉。
这样就可以得到答案了。

//使用映射关系来解决问题
#include 
#include
#include
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
const int maxn=4e5+5;
int fac[]={1,1,2,6,24,120,720,5040,40320,362880};//康拖展开判重
int name[10],index,root[10];
int dex[4]={1,0,0,-1};//方向数组
int dey[4]={0,-1,1,0};
char des[4]={'d','l','r','u'};
int used[15][maxn],roots,num=1;
int top[15][maxn];//top代表第kind中情况的cantor()的上一个值
struct filles{
    int name[10];
    int x;
    int cant;
};
int cantor(int s[])//康拖展开求该序列的hash值
{
    int sum=0;
    for(int i=0;i<9;i++)
    {
        int num=0;
        for(int j=i+1;j<9;j++)
          if(s[j]<s[i])num++;
        sum+=(num*fac[9-i-1]);
    }
    return sum+1;
}
void printco(int kind,int s){//用于打印代表的路径
    string path;
    //cout<
    while(s!=0){
        path=des[used[kind][s]-1]+path;
        s=top[kind][s];
    }
    printf("Case %d: %d\n",num++,path.size()-1);
    for(int i=1;i<path.size();i++)printf("%c",path[i]);
    printf("\n");
}
void bfs(int r){
    queue<filles>sl;
    filles a,b;
    a.name[r]=0;
    int k=1;
    for(int i=0;i<9;i++){
        if(i==r)continue;
        else{
            a.name[i]=k++;
        }
    }
    a.x=r;
    a.cant=cantor(a.name);
    used[r][a.cant]=1;
    sl.push(a);
    while(!sl.empty()){
        b=sl.front();
        sl.pop();
        int x=b.x/3;
        int y=b.x%3;
        for(int i=0;i<4;i++){
            int xx=x+dex[i];
            int yy=y+dey[i];
            //cout<
            if(xx<0||xx>2||yy<0||yy>2)continue;
            a=b;
            a.x=xx*3+yy;
            a.name[b.x]=a.name[a.x];
            a.name[a.x]=0;
            int s=cantor(a.name);
            if(!used[r][s]){
                top[r][s]=a.cant;
                a.cant=s;
                used[r][s]=i+1;//保存a点走的方向,同时便于判断是否查询过,使用i+1
                sl.push(a);
            }
        }
    }
}
int main(){
    int T;
    memset(used,0,sizeof(used));
    for(int i=0;i<9;i++){
        bfs(i);
    }
    scanf("%d",&T);
    cin.get();
    char ch;
    while(T--){
        int k=1;
        for(int i=0;i<9;i++){
            scanf("%c",&ch);
            if(ch=='X'){
                name[0]=0;
                index=i;
            }
            else{
                name[ch-'0']=k++;
            }
        }
        cin.get();
        for(int i=0;i<9;i++){
            scanf("%c",&ch);
            if(ch=='X'){
                root[i]=0;
            }
            else root[i]=name[ch-'0'];
        }
        cin.get();
        roots=cantor(root);
        printco(index,roots);

    }
}

你可能感兴趣的:(HDU,3567,Eight,II,八数码(2),c++)