HDU 1043 Eight

題目描述:

The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as:

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15  x


where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:

 1  2  3  4     1  2  3  4     1  2  3  4     1  2  3  4
 5  6  7  8     5  6  7  8     5  6  7  8     5  6  7  8
 9  x 10 12     9 10  x 12     9 10 11 12     9 10 11 12
13 14 11 15    13 14 11 15    13 14  x 15    13 14 15  x
            r->            d->            r->


The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.

Input

You will receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x'. For example, this puzzle

1 2 3
x 4 6
7 5 8

is described by this list:

1 2 3 x 4 6 7 5 8

Output

You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line. Do not print a blank line between cases.

Sample Input

2  3  4  1  5  x  7  6  8

Sample Output

ullddrurdllurdruldr

解題報告:

1:我這裏的做法是逆向bfs,從12345678x這個狀態開始bfs,到達每一個點的時候將操作存起來。

2:方向數組要和字典數組反着來,最後答案要逆向輸出。可能我這題

代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 4e5+10;
ll Map[3][3], fac[10], dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
char dic[4] = {'u', 'd', 'l', 'r'};
struct point{
    ll num, x, y;
    vector<char> V;
}p1, p2;
vector<char> VV[N];
map<ll, ll> M;
queue<point> Q;
inline void init(){
    fac[0] = fac[1] = 1;
    for(ll i=2; i<10; ++i)fac[i] = fac[i-1]*i;
}
inline ll Hash(){
    ll ans = 0;
    for(ll i=0; i<9; ++i){
        ll cnt = 0;
        for(ll j=i+1; j<9; ++j){
            if(Map[i/3][i%3] > Map[j/3][j%3])++cnt;
        }
        ans += cnt*fac[8-i];
    }
    return ans;
}
inline void toArray(ll x){
    ll visit[10];
    memset(visit, 0, sizeof(visit));
    for(ll i=8; i>=0; --i){
        ll cnt = 0, k, idx = 8-i;
        for(k=1; k<=9; ++k){
            if(!visit[k])++cnt;
            if(cnt == x/fac[i]+1)break;
        }
        Map[idx/3][idx%3] = k, x %= fac[i];
        visit[k] = 1;
    }
}
void bfs(ll sX, ll sY, ll s){
    p1.x = sX, p1.y = sY, p1.num = s, M[s] = 1;
    Q.push(p1);
    while(!Q.empty()){
        p1 = Q.front();
        for(ll i=0; i<4; ++i){
            p2.x = p1.x+dir[i][0];
            p2.y = p1.y+dir[i][1];
            if(p2.x < 0 || p2.y < 0 || p2.x >= 3 || p2.y >= 3)continue;
            toArray(p1.num);
            swap(Map[p2.x][p2.y], Map[p1.x][p1.y]);
            p2.num = Hash();
            if(M[p2.num])continue;
            p2.V = p1.V, p2.V.push_back(dic[i]);
            VV[p2.num] = p2.V, M[p2.num] = 1;
            Q.push(p2);
        }
        Q.pop();
    }
}
int main(){
    char x[2];
    init(), bfs(2, 2, 0);
    while(~scanf("%s", x)){
        Map[0][0] = x[0] == 'x' ? 9 : x[0]-'0';
        for(ll i=1; i<9; ++i){
            scanf("%s", x);
            Map[i/3][i%3] = x[0] == 'x' ? 9 : x[0]-'0';
        }
        ll s = Hash();
        if(VV[s].size() == 0 && s != 0)printf("unsolvable");
        else {
            ll len = VV[s].size();
            for(ll i=len-1; i>=0; --i)printf("%c", VV[s][i]);
        }
        puts("");
    }
    return 0;
}

 

發佈了211 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章