PKU-1077 Eight (八数码单向BFS+HASH)

来源:互联网 发布:尚德机构 面试题 Java 编辑:程序博客网 时间:2024/05/22 12:25

原题链接 http://poj.org/problem?id=1077  

Eight

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 Code

/*PKU 只有一组数据,所以正向搜索*/#include <iostream>#include <queue>using namespace std;#define Max 365000   // 9! = 362880//x,y记录当前X所在位置,state保存当前排列状态//cantorValue记录当前状态对应的康托展开值struct node {int x, y;char state[10];int cantorValue;};//searched保存当前状态是否出现过//parent保存前一状态//opration保存前一状态到当前状的操作(上下左右)int searched[Max], parent[Max], opration[Max];int index;queue<node> Q;node Next, Top;int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1};/*康托展开:X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0!例:3 5 7 4 1 2 9 6 8 展开为 98884。因为X=2*8!+3*7!+4*6!+2*5!+0*4!+0*3!+2*2!+0*1!+0*0!=98884.解释: 排列的第一位是3,3以后的序列中比3小的数有2个1,2,以这样的数开始的排列有8!个,因此第一项为2*8!排列的第二位是5,5以后的序列中比5小的数有3个4、1、2,这样的排列有7!个,因此第二项为3*7!  …………以此类推,直至0*0!*/int Cantor(char * buf, int len) {     //bit记录之后的序列中比当前位小的值的个数int cantor[9] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};int i, j, bit;int result = 0;for (i = 0; i < len; i ++) {bit = 0;for (j = i + 1; j < len; j++) {if (buf[i] > buf[j]) {bit ++;}}result += (bit * cantor[len - i - 1]);}return result;}//判断是否越界bool isInside(int x, int y) {if (x < 0 || y < 0 || x > 2 || y > 2) {return false;}return true;} void BFS(node start) {int i, j, nextX, nextY;int a, b, step[30];char temp;while (!Q.empty()) {Q.pop();}Q.push(start);while (!Q.empty()) {Top = Q.front();Q.pop();for (i = 0; i < 4; i++) {nextX = Top.x + dir[i][0];nextY = Top.y + dir[i][1];if (!isInside(nextX, nextY)) {continue;}            //下一个状态Next.x = nextX;Next.y = nextY; for (j = 0; j < 9; j ++) {Next.state[j] = Top.state[j];}a = Next.x * 3 + Next.y;b = Top.x * 3 + Top.y;temp = Next.state[a];Next.state[a] =  Next.state[b];Next.state[b] = temp;Next.cantorValue = Cantor(Next.state, 9);if (!searched[Next.cantorValue]) {searched[Next.cantorValue] = 1;parent[Next.cantorValue] = Top.cantorValue;opration[Next.cantorValue] = i;//123456789的康托展开为0if (Next.cantorValue == 0) {                    int len = 0;int cur = Next.cantorValue;//回溯路径while (parent[cur] != -1) {step[len ++] = opration[cur];cur = parent[cur];}for (j = len - 1; j >= 0; j--) {switch (step[j]){case 0: printf("d");break;case 1:printf("u");break;case 2:printf("r");break;case 3:printf("l");break;}}printf("\n");break;} else {Q.push(Next);}}}}}int main() {int i;char init[10];index = 0;memset(searched, 0, sizeof(searched));node start;for (i = 0; i < 9; i++) {cin >> init[i];if (init[i] == 'x') {init[i] = '0' + 9;start.x = i / 3;start.y = i % 3;}}for (i = 0; i < 9; i++) {start.state[i] = init[i];}start.cantorValue = Cantor(init, 9);    searched[start.cantorValue] = 1;parent[start.cantorValue] = -1;opration[start.cantorValue] = -1;BFS(start);return 0;}

 

 

HDU该题链接 http://acm.hdu.edu.cn/showproblem.php?pid=1043

Source Code

/*hdu测试数据有多组,正向搜索会TLE,所以用反向搜索基本思想:从目标状态反向搜索,把所有可以达到的状态保存下来,输入初始状态后直接输出结果*/#include <iostream>#include <queue>using namespace std;#define Max 365000   // 9! = 362880//x,y记录当前X所在位置,state保存当前排列状态//cantorValue记录当前状态对应的康托展开值struct node {int x, y;char state[10];int cantorValue;};//searched保存当前状态是否出现过//parent保存前一状态//opration保存前一状态到当前状的操作(上下左右)int searched[Max], parent[Max], opration[Max];int index;queue<node> Q;node Next, Top;int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1};bool isSolved;/*康托展开:X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0!例:3 5 7 4 1 2 9 6 8 展开为 98884。因为X=2*8!+3*7!+4*6!+2*5!+0*4!+0*3!+2*2!+0*1!+0*0!=98884.解释: 排列的第一位是3,3以后的序列中比3小的数有2个1,2,以这样的数开始的排列有8!个,因此第一项为2*8!排列的第二位是5,5以后的序列中比5小的数有3个4、1、2,这样的排列有7!个,因此第二项为3*7!  …………以此类推,直至0*0!*/int Cantor(char * buf, int len) {     //bit记录之后的序列中比当前位小的值的个数int cantor[9] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};int i, j, bit;int result = 0;for (i = 0; i < len; i ++) {bit = 0;for (j = i + 1; j < len; j++) {if (buf[i] > buf[j]) {bit ++;}}result += (bit * cantor[len - i - 1]);}return result;}//判断是否越界bool isInside(int x, int y) {if (x < 0 || y < 0 || x > 2 || y > 2) {return false;}return true;} void BFS(node start) {int i, j, nextX, nextY;int a, b;char temp;while (!Q.empty()) {Q.pop();}Q.push(start);while (!Q.empty()) {Top = Q.front();Q.pop();for (i = 0; i < 4; i++) {nextX = Top.x + dir[i][0];nextY = Top.y + dir[i][1];if (!isInside(nextX, nextY)) {continue;}            //下一个状态Next.x = nextX;Next.y = nextY; for (j = 0; j < 9; j ++) {Next.state[j] = Top.state[j];}a = Next.x * 3 + Next.y;b = Top.x * 3 + Top.y;temp = Next.state[a];Next.state[a] =  Next.state[b];Next.state[b] = temp;Next.cantorValue = Cantor(Next.state, 9);if (!searched[Next.cantorValue]) {searched[Next.cantorValue] = 1;parent[Next.cantorValue] = Top.cantorValue;opration[Next.cantorValue] = i;Q.push(Next);}}}}int main() {int i, j;char init[10];node start;//目标状态开始向四周搜索memset(searched, 0, sizeof(searched));memset(parent, -1, sizeof(parent));memset(opration, -1, sizeof(opration));    start.x = 2;start.y = 2;for (i = 1; i <= 9; i++) {start.state[i - 1] = '0' + i;}start.cantorValue = 0;BFS(start);while (cin >> init[0]) {if (init[0] == 'x') {init[0] = '0' + 9;}for (i = 1; i < 9; i++) {cin >> init[i];if (init[i] == 'x') {init[i] = '0' + 9;}}int index = Cantor(init, 9);if (parent[index] == -1) {printf("unsolvable\n");} else {int step[100];int len = 0;while (parent[index] != 0) {step[len ++] = opration[index];index = parent[index];}step[len ++] = opration[index];//方向和正向搜索相反for (i = 0; i < len; i++) {switch (step[i]){case 0: printf("u");break;case 1:printf("d");break;case 2:printf("l");break;case 3:printf("r");break;}}printf("\n");}}return 0;}
原创粉丝点击