poj 1077(BFS预处理+康托展开)

Eight
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29935   Accepted: 13029   Special Judge

Description

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 a description of a configuration of the 8 puzzle. The 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.

Sample Input

 2  3  4  1  5  x  7  6  8 

Sample Output

ullddrurdllurdruldr

题意:经典八数码
题解:预处理终点到所有状态的路径。康拓展开保存状态
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N = 450000;
int fab[] = {1,1,2,6,24,120,720,5040,40320};
bool vis[N];
struct Node{
    int a[15];
    int Hash;
    int _x; ///x所在位置
};
struct Way{
    char c;
    int pre;
}way[N];
int contor(Node s){
    int sum = 0;
    for(int i=9;i>=1;i--){
        int cnt = 0;
        for(int j=i-1;j>=1;j--){
            if(s.a[i]<s.a[j]) cnt++;
        }
        sum+=fab[i-1]*cnt;
    }
    return sum;
}
int dir[][2] = {{-1,0},{1,0},{0,-1},{0,1}}; ///上下左右
bool change(Node &s,int _x,int k){
    int x = (_x-1)/3+1;
    int y = _x%3==0?3:_x%3;
    int nextx = x+dir[k][0];
    int nexty = y+dir[k][1];
    if(nextx<1||nexty>3||nexty<1||nexty>3) return false;
    swap(s.a[_x],s.a[(nextx-1)*3+nexty]);
    s._x = (nextx-1)*3+nexty;
    return true;
}
void bfs(){
    for(int i=0;i<N;i++){
        way[i].pre = -1;
    }
    memset(vis,false,sizeof(vis));
    Node s;
    for(int i=1;i<=9;i++){
        s.a[i] = i;
    }
    s.Hash = 0,s._x = 9;
    vis[0] = 1;
    queue<Node> q;
    q.push(s);
    while(!q.empty()){
        Node now = q.front();
        q.pop();
        Node next;
        next = now;
        if(change(next,next._x,0)){
            int k = contor(next);
            if(!vis[k]){
                vis[k] = true;
                next.Hash = k;
                way[k].pre = now.Hash;
                way[k].c = 'd';
                q.push(next);
            }
        }
        next = now;
        if(change(next,next._x,1)){
            int k = contor(next);
            if(!vis[k]){
                vis[k] = true;
                next.Hash = k;
                way[k].pre = now.Hash;
                way[k].c = 'u';
                q.push(next);
            }
        }
        next = now;
        if(change(next,next._x,2)){
            int k = contor(next);
            if(!vis[k]){
                vis[k] = true;
                next.Hash = k;
                way[k].pre = now.Hash;
                way[k].c = 'r';
                q.push(next);
            }
        }
        next = now;
        if(change(next,next._x,3)){
            int k = contor(next);
            if(!vis[k]){
                vis[k] = true;
                next.Hash = k;
                way[k].pre = now.Hash;
                way[k].c = 'l';
                q.push(next);
            }
        }
    }
}
char str[10];
char ans[10000];
int t = 0;
void dfs(int k){
    if(way[k].pre==-1) return;
    dfs(way[k].pre);
    ans[t++]=way[k].c;
}
int main()
{
    bfs();
    while(scanf("%s",str)!=EOF){
        Node s;
        s.a[1] = (str[0]=='x')?9:str[0]-'0';
        for(int i=2;i<=9;i++){
            scanf("%s",str);
            s.a[i] = (str[0]=='x')?9:str[0]-'0';
        }
        int k = contor(s);
        ans;
        t = 0;
        dfs(k);
        if(t==0){
            printf("unsolvable\n");
            continue;
        }
        for(int i=t-1;i>=0;i--){
            printf("%c",ans[i]);
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2016-08-09 16:57  樱花庄的龙之介大人  阅读(250)  评论(0编辑  收藏  举报