HDU3567(Eight II)

Eight II

題目傳送門

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

思路

八數碼升級版,這次是最終目標不一致給定起始情況和終點情況,問從起始到終點需要多少步以及步數相同的情況下最小的字典序。還是採用離線打表的方式,不過這次打表需要打出大約37*10萬種情況。還需要結合哈希映射,bfs打表 + 康託展開 + hash,兩道八數碼題折騰了一天,錯誤提交加起來有3頁多吧。這道題不適合新手玩家做,做hash之前把臉和手洗乾淨不然容易出錯,心態擺正就沒什麼問題。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std; 
int sum[10] = {1,1,2,6,24,120,720,5040,40320,362880};
int dx[] = {0,-1,1,0};            
int dy[] = {1,0,0,-1};             
char dir[5] = "dlru";
char s[10][10] = {"012345678", "102345678", "120345678", "123045678",
"123405678","123450678", "123456078", "123456708", "123456780"};
struct node{
    int pre;
    char way;
    int step;
}Node[370000][11];
struct info{
    int ct;
    char state[10];
    int num;
};
queue<info>q;
int Cantor(char s[])
{
    int result = 0;
    for(int i = 0;i < 9;i++){
        int cnt = 0;
        for(int j = i+1;j < 9;j++){
            if(s[i] > s[j]){
                cnt++;
            } 
        }
        result += cnt * sum[8-i];
    }
    return result + 1;
}
void bfs(int x)
{
    while(!q.empty()){
        info ptr = q.front(),p;
        q.pop();
        for(int i = 0;i < 4;i++){
            int nx = ptr.num%3 + dx[i];
            int ny = ptr.num/3 + dy[i];
            int nz = nx + 3*ny;
            if(nx < 0 || nx >= 3 || ny < 0 || ny >= 3){
                continue;
            }
            memcpy(p.state,ptr.state,sizeof(p.state));
            swap(p.state[nz],p.state[ptr.num]);
            p.num = nz;
            p.ct = Cantor(p.state);
            if(Node[p.ct][x].pre == -1){
                Node[p.ct][x].step = Node[ptr.ct][x].step + 1;
                Node[p.ct][x].pre = ptr.ct;
                Node[p.ct][x].way = dir[i];
                q.push(p);
            }
        }
    }
}
void clear_queue(int j)
{
    while(!q.empty()){
        q.pop();
    }
    for(int i = 0;i < 370000;i++){
        Node[i][j].pre = -1;
    }
}
void slove()
{
    for(int i = 0;i < 9;i++){
        clear_queue(i);
        info p;
        memcpy(p.state,s[i],sizeof(p.state));
        p.ct = Cantor(s[i]);
        p.num = i;
        Node[p.ct][i].step = 0;
        Node[p.ct][i].pre = 0;
        q.push(p);
        bfs(i);
    }
}
int main()
{
//    freopen("C:\\Users\\Administrator\\Desktop\\input.txt","r",stdin);
//    freopen("C:\\Users\\Administrator\\Desktop\\output.txt","w",stdout);
    slove();
    int t;
    scanf("%d",&t);
    for(int j = 1;j <= t;j++){
        char str1[100],str2[100];
        int pos;
        scanf("%s%s",str1,str2);
        for(int i = 0;i < 9;i++){
            if(str1[i] == 'X'){
                str1[i] = '0';
                pos = i;
            }
            if(str2[i] == 'X'){
                str2[i] = '0';
            }
        }
        int hash[10];
        for(int i = 0;i < 9;i++){			//哈希處理
            hash[str1[i] - '0'] = s[pos][i] - '0';
        } 
        for(int i = 0;i < 9;i++){			
            str2[i] = hash[str2[i] - '0'] + '0';
        }
        str2[9] = '\0';
        int ctvalue = Cantor(str2);
        int k = Node[ctvalue][pos].step;
        printf("Case %d: %d\n",j,k);
        //printf("%c",Node[ctvalue][pos].way);
        int p = k;
        char ans[50];
        while(ctvalue){
            ans[--k] = Node[ctvalue][pos].way;
            ctvalue = Node[ctvalue][pos].pre;
        }
        ans[p] = '\0';
        printf("%s\n",ans);
    }
    return 0;
}

願你走出半生,歸來仍是少年~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章