poj 1077 Eight(BFS + 康托判重)

Eight
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23233   Accepted: 10249   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

Source

South Central USA 1998


题目大意:八数码经典问题,类似于一个拼图,给你一个初始状态,通过上下左右四种移动方式使之成为12345678x
解题思路:可以用9或0代替x,本题的关键在于9!如何判重??
我们可以用康托展开,什么是康托展开,就是求某一个全排列在所有的全排列中是第几个。。比如12345在5!的全排列中是第一个,康托展开就是0,54321则是5!-1,那么我们如何求呢?我们可以从左往右一位一位运算,再比如43512,第一位是4,也就是说1,2,3都在这一位上排过了,就是3*4!,第二位是3,表示1,2在该位排过,再加2*3!……以此类推。
关于路径输出,再开辟一个方向节点记录递归输出即可。
另外还要注意本题的Special Judge,因为搜索方向的顺序不定,所以和样例的输出也不一定完全一样,只要能到达目标状态即可。

#include
#include
#include
#include
using namespace std;
const int N = 5;
const int M = 400000;

struct Point{
    int x;
    int num;
    char str[10];
    int pre;
    int dir;
}fr, next, start, prev[M];
int mp[N][N];
int cv[9][4]={-1, 1,-1, 3,//l r u d
	           0, 2,-1, 4,
	           1,-1,-1, 5,
	          -1, 4, 0, 6,
	           3, 5, 1, 7,
	           4,-1, 2, 8,
	          -1, 7, 3,-1,
	           6, 8, 4,-1,
	           7,-1, 5,-1};
int vis[M];
int fac[8] = {1, 2, 6, 24, 120, 720, 5040, 40320};
char buf[10];
char dirr[4] = {'l', 'r', 'u', 'd'};

int cantor(char *s){
    int sum = 0;
    for(int i = 0; i < 8; i++){
        int c = 0;
        for(int j = i + 1; j < 9; j++) if(s[i] > s[j]) c++; // 比它小的数表示已经在这位置填过了
        sum += c * fac[7 - i];
    }
    return sum;
}

void print(int y){
    if(y == start.num) return;
    print(prev[y].pre);
    printf("%c", prev[y].dir);
}

void bfs(){
    queue Q;
    Q.push(start);
    while(!Q.empty()){
        fr = Q.front();
        Q.pop();
        if(fr.num == 0){
            print(fr.num);
            putchar('\n');
            return;
        }
        for(int i = 0; i < 4; i++){
            if(cv[fr.x][i] != -1){
                memset(buf, '\0', sizeof(buf));
                memcpy(buf, fr.str, 9);
                char t = buf[fr.x];
                buf[fr.x] = buf[cv[fr.x][i]];
                buf[cv[fr.x][i]] = t;
                int cant = cantor(buf);
                if(!vis[cant]){
                 //   cout << buf << endl;
                    next.x = cv[fr.x][i];
                    memcpy(next.str, buf, 9);
                    next.num = cant;
                    vis[cant] = 1;
                    Q.push(next);
                    prev[next.num].pre = fr.num;
                    prev[next.num].dir = dirr[i];
                }
            }

        }
    }
    puts("unsolvable");
    return;
}

int main(){
    char c;
    for(int i = 0; i < 9; i++){
        while((c = getchar()) == ' ');
        if(c == 'x'){
            start.str[i] = '9';
            start.x = i;
        }
        else start.str[i] = c;
    }
  //  cout << start.x << endl;
  //  cout << start.str << endl;
    start.num = cantor(start.str);
    memset(vis, 0, sizeof(vis));
    vis[start.num] = 1;
    bfs();
    return 0;
}


/*
2 3 4 1 5 x 7 6 8
1 2 3 4 5 6 7 x 8
1 2 3 4 5 x 7 8 6
1 2 3 4 x 5 7 8 6
1 2 3 4 5 x 6 7 8
*/

你可能感兴趣的:(搜索(初级))