POJ 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:
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:
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.
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
is described by this list:
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
【随笔】这题跟杭电那题类似,不同是这题只有一个Case输入,时间限制为1000MS,修改了一下以前的代码还是超时了,印象中之前看过八数码的八境界这篇文章,可惜到现在都还未了解过到底为什么用上STL和string字符串会消耗那么多的时间,现在应该有那个概念了,希望那天能把这点知识补上了,后来替代了队列用数组,去字符串用字符,375ms过了这题,因为 在杭电上写过这题,所以就不再详细写,解题思路在这里
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 #define MAXN 362888 7 #define MAXSIZE 1000000 8 #define SIZE 9 9 10 using namespace std; 11 12 typedef int State[SIZE]; 13 int dir[][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; 14 char dirFlag[] = "lrud"; 15 16 State queuing[MAXSIZE]; 17 int load[MAXN][2]; 18 bool visit[MAXN]; 19 char res[SIZE*6]; 20 21 int factory[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; 22 int aim = 46233, start; 23 int input[SIZE]; 24 25 int try_to_insert(int s[]) 26 { 27 int sum = 0; 28 for(int i=0; i<SIZE; ++i) 29 { 30 int cnt = 0; 31 for(int j=i+1; j<SIZE; ++j) 32 if(s[i]>s[j]) ++cnt; 33 sum += cnt*factory[SIZE-i-1]; 34 } 35 return sum; 36 } 37 38 bool Traverse() 39 { 40 memset(visit, false, sizeof(visit)); 41 memset(load, -1, sizeof(load)); 42 int front = 1, rear = 2; 43 memcpy(queuing[front], input, sizeof(input)); 44 while(front < rear) 45 { 46 int z; 47 State& s = queuing[front]; 48 for(z=0; z<SIZE; ++z) if(!s[z]) break; 49 int x = z/3, y = z%3; 50 for(int d=0; d<4; ++d) 51 { 52 int newx = x + dir[d][0]; 53 int newy = y + dir[d][1]; 54 int newz = newx * 3 + newy; 55 if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3) 56 { 57 State t; 58 memcpy(t, s, sizeof(s)); 59 t[newz] = s[z]; 60 t[z] = s[newz]; 61 int elem = try_to_insert(s); 62 int adr = try_to_insert(t); 63 if(!visit[adr]) 64 { 65 visit[adr] = true; 66 load[adr][0] = elem, load[adr][1] = d; 67 memcpy(queuing[rear++], t, sizeof(t)); 68 if(adr == aim) return true; 69 } 70 } 71 } 72 front++; 73 } 74 return false; 75 } 76 77 int main() 78 { 79 char ch; 80 for(int i=0; i<SIZE; ++i) 81 { 82 cin>>ch; 83 if(ch == 'x') ch = '0'; 84 input[i] = ch - '0'; 85 } 86 start = try_to_insert(input); 87 if(start == aim) 88 { 89 cout<<endl; 90 return 0; 91 } 92 else 93 { 94 if(Traverse()) 95 { 96 int cnt; 97 for(cnt=0; aim != start; cnt++) 98 { 99 res[cnt] = dirFlag[load[aim][1]]; 100 aim = load[aim][0]; 101 } 102 for(cnt -= 1; cnt >= 0; --cnt) 103 cout<<res[cnt]; 104 cout<<endl; 105 } 106 else cout<<"unsolvable"<<endl; 107 } 108 return 0; 109 }
更多内容请关注个人微信公众号 物役记 (微信号:materialchains)
作者:雪影蓝枫
本文版权归作者和博客园共有,欢迎转载,未经作者同意须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。