HDU3567:Eight II(康拓展開+預先處理)

來源:互聯網
上載者:User

標籤:搜尋   康拓   hdu   

Problem DescriptionEight-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.
 
InputThe 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.
 
OutputFor 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
212X45378612345678X564178X237568X4123
 
Sample Output
Case 1: 2ddCase 2: 8urrulldr
 

題意:由字串1變成字串2所需要在最小步數的情況下輸出其字典序最小的方案思路:八數位問題,處理這種問題首先想到的方法自然是康拓,我們可以先枚舉X不同位置的9中方案,用bfs搜出這種方案到其他所有方案的移動方法,那麼在後面我們就可以直接得出答案,使用逆推的方法
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#include <stack>#include <string>using namespace std;struct node{    int x,y;    char map[5][5];    node() {}    node(char *s)    {        int i,j;        int xx = 0,yy = 0;        for(i = 0; i<strlen(s); i++)        {            map[xx][yy] = s[i];            if(s[i] == 'X')            {                x = xx;                y = yy;            }            yy++;            if(yy == 3)            {                xx++;                yy = 0;            }        }    }};node s;char str[20];int num[20],hash[10];bool vis[500000];int pre[10][500000],ans[10][500000];int to[4][2] = {1,0,0,-1,0,1,-1,0};char way[10] = "dlru";int solve(node a)//康拓{    int i,j,k,cnt,ans = 0;    int b[20];    for(i = 0; i<3; i++)    {        for(j = 0; j<3; j++)        {            b[3*i+j] = a.map[i][j];            cnt = 0;            for(k = 3*i+j-1; k>=0; k--)            {                if(b[k]>b[3*i+j])                    cnt++;            }            ans+=hash[3*i+j]*cnt;        }    }    return ans;}void bfs(int p){    memset(pre[p],-1,sizeof(pre[p]));    memset(vis,false,sizeof(vis));    node a,next;    queue<node> Q;    Q.push(s);    vis[solve(s)] = true;    while(!Q.empty())    {        a = Q.front();        Q.pop();        int sa = solve(a);        for(int i = 0; i<4; i++)        {            next = a;            next.x+=to[i][0];            next.y+=to[i][1];            if(next.x<0 || next.x>2 || next.y<0 || next.y>2)                continue;            next.map[a.x][a.y] = next.map[next.x][next.y];            next.map[next.x][next.y] = 'X';            int sb = solve(next);            if(vis[sb])                continue;            vis[sb] = true;            pre[p][sb] = sa;            ans[p][sb] = way[i];            Q.push(next);        }    }}int main(){    int t,i,j,k,cas = 1;    hash[0] = 1;    for(i = 1; i<10; i++)        hash[i] = hash[i-1]*i;    s = node("X12345678");    bfs(0);    s = node("1X2345678");    bfs(1);    s = node("12X345678");    bfs(2);    s = node("123X45678");    bfs(3);    s = node("1234X5678");    bfs(4);    s = node("12345X678");    bfs(5);    s = node("123456X78");    bfs(6);    s = node("1234567X8");    bfs(7);    s = node("12345678X");    bfs(8);    scanf("%d",&t);    while(t--)    {        scanf("%s",str);        int p;        for(i = 0,j = 0; i<9; i++)//儲存位置,因為前面預先處理的都是位置        {            if(str[i]=='X') p = i;            else                num[str[i]-'0'] = j++;        }        scanf("%s",str);        for(i = 0; i<9; i++)//求出目標狀態每個數在原狀態的位置        {            if(str[i]=='X')                continue;            str[i] = num[str[i]-'0']+'1';        }        s = node(str);//由目標態逆推到初始態        int sum = solve(s);        string ss="";        while(sum!=-1)        {            ss+=ans[p][sum];            sum = pre[p][sum];        }        printf("Case %d: %d\n",cas++,ss.size()-1);        for(i = ss.size()-2; i>=0; i--)//由於方案是逆推,輸出也要逆推            printf("%c",ss[i]);        printf("\n");    }    return 0;}


HDU3567:Eight II(康拓展開+預先處理)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.