Eight(bfs+全排列的哈希函数)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22207   Accepted: 9846   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

题意:这是一个8数码问题,听着好高端的样子;就是给你一个3*3的矩阵,包括1~8和x;例
1  2  3 

x 4 6
7 5 8 问最少需要变换x几步成为

1 2 3
4 5 6
7 8 x 的形式;

这题的关键是找到一个哈希函数,使得矩阵形成的排列与一个自然数一一对应,这里采用的是全排列的哈希函数,另一个就是BFS加打印路径了;

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<queue>
  4 #include<iostream>
  5 using namespace std;
  6 
  7 const int maxn = 362881;//根据全排列的哈希函数,n+1个数的排列可以对应n个数的多进制形式,这里九个数对应多进制的最大值为9!-1;
  8 int factorial[9] = {1,1,2,6,24,120,720,5040,40320};
  9 int pow[9] = {100000000,10000000,1000000,100000,10000,1000,100,10,1};
 10 int head,tail;
 11 bool vis[maxn];
 12 
 13 struct node
 14 {
 15     char status;
 16     int id,num,pre;
 17 }que[maxn];
 18 
 19 int hash(int num)//全排列的哈希函数
 20 {
 21     int a[10],key,i,j,c;
 22     for(i = 0; i < 9; i++)
 23     {
 24         a[i] = num%10;//a数组倒着存的num,所以求逆序数的时候条件是a[j]<a[i];
 25         num = num/10;
 26     }
 27     key = 0;
 28     for(i = 1; i < 9; i++)
 29     {
 30         for(j = 0,c = 0; j < i; j++)
 31         {
 32             if(a[j] < a[i])
 33                 c++;
 34         }
 35         key += c*factorial[i];
 36     }
 37     return key;
 38 }
 39 
 40 void change(int num,int a,int b,char status)//a位置上的数和b位置上的数互换;
 41 {
 42     int n1,n2;
 43     n1 = num/pow[a]%10;
 44     n2 = num/pow[b]%10;
 45     num = num - (n1-n2)*pow[a] + (n1-n2)*pow[b];
 46     int key = hash(num);
 47     if(!vis[key])
 48     {
 49         vis[key] = true;
 50         que[tail].num = num;
 51         que[tail].id = b;
 52         que[tail].status = status;
 53         que[tail++].pre = head;
 54     }
 55 }
 56 
 57 //打印路径
 58 void print(int head)
 59 {
 60     char s[100];
 61     int c = 0;
 62     while(que[head].status != 'k')
 63     {
 64         s[c++] = que[head].status;
 65         head = que[head].pre;
 66     }
 67     s[c] = '';
 68     for(int i = c-1; i >= 0; i--)
 69     {
 70         printf("%c",s[i]);
 71     }
 72     printf("
");
 73 }
 74 
 75 int main()
 76 {
 77     char c;
 78     int num,id,t;
 79 
 80     num = 0;
 81     for(int i = 0; i < 9; i++)
 82     {
 83         cin>>c;
 84         if(c == 'x')
 85         {
 86             t = 9;
 87             id = i;
 88         }
 89         else t = c-'0';
 90         num = 10*num+t;
 91     }
 92     bool flag = false;
 93     memset(vis,false,sizeof(vis));
 94 
 95     head = 0;
 96     tail = 1;
 97     que[0].id = id;
 98     que[0].num = num;
 99     que[0].status = 'k';
100 
101     while(head < tail)
102     {
103         num = que[head].num;
104         id = que[head].id;
105         if(num == 123456789)
106         {
107             flag = true;
108             break;
109         }
110         if(id > 2)
111             change(num,id,id-3,'u');
112 
113         if(id < 6)
114             change(num,id,id+3,'d');
115 
116         if(id%3 != 0)
117             change(num,id,id-1,'l');
118         if(id%3 != 2)
119             change(num,id,id+1,'r');
120         head++;
121 
122     }
123     if(flag) print(head);
124     else printf("unsolvable
");
125     return 0;
126 }
View Code
原文地址:https://www.cnblogs.com/LK1994/p/3383060.html