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 1213 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 1213 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.
InputYou will receive, several descriptions of configuration of the 8 puzzle. One 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
OutputYou 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. Do not print a blank line between cases.
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
题意:
给你一个3*3的九方格,每个格子中放入‘1’~‘8’和‘x‘中的一个,并且每个格子中的字符不相同;
1 2 3
x 4 6
7 5 8
上面的格子序列可以通过输入 12346758来表示。
其中,’能和与它相邻的上下左右四个格子相交换;交换方式用‘u‘,‘d‘,‘l‘,‘r‘表示。问能不能通过有限次的交换,使之变为
1 2 3
4 5 6
7 8 x
即 12345678x
若能输出每一步的路径,若不能输出``unsolvable‘‘;
#include <iostream> #include<queue> #include<vector> #include<algorithm> #include<cstring> using namespace std; int fac[]={1,1,2,6,24,120,720,5040,40320,362880}; const int maxn=370000; bool vis[maxn]; vector<char>Path[maxn];//若用string来保存,则会MLE int dxy[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//u,d,l,r; char index[5]="durl";//要与上面相反,因为是反向搜索 int aim=46234;//123456780的hash值 struct node { int loc;//0的位置 int state;//哈希值 int s[9]; vector<char>path; }; int cantor(int *s)//康拓展开,求hash值 { int ans=0; for(int i=0;i<9;i++) { int num=0; for(int j=i+1;j<9;j++) { if(s[j]<s[i])num++; } ans+=(num*fac[9-i-1]); } return ans+1; } void bfs() { memset(vis,0,sizeof(vis)); node cur,nex; for(int i=0;i<8;i++)cur.s[i]=i+1; cur.s[8]=0; cur.loc=8; cur.state=aim; cur.path.clear(); queue<node>q; q.push(cur); vis[aim]=1; Path[aim].clear(); while(!q.empty()) { cur=q.front(); q.pop(); int x=cur.loc/3,y=cur.loc%3; for(int i=0;i<4;i++) { int dx=x+dxy[i][0],dy=y+dxy[i][1]; if(dx<0||dx>2||dy<0||dy>2)continue; nex=cur; nex.loc=dx*3+dy; nex.s[cur.loc]=nex.s[nex.loc];//先交换 nex.s[nex.loc]=0;//再赋值 nex.state=cantor(nex.s); if(!vis[nex.state]) { vis[nex.state]=1; nex.path.insert(nex.path.begin(),index[i]);//插入到第一个位置 q.push(nex); Path[nex.state]=nex.path; } } } } int main() { char ch; node cur; vector<char>v; vector<char>::iterator it; bfs(); while(cin>>ch) { if(ch==‘x‘){cur.s[0]=0;cur.loc=0;} else cur.s[0]=ch-‘0‘; for(int i=1;i<9;i++) { cin>>ch; if(ch==‘x‘) { cur.s[i]=0; cur.loc=i; } else cur.s[i]=ch-‘0‘; } cur.state=cantor(cur.s); if(vis[cur.state]) { v=Path[cur.state]; for(it=v.begin();it!=v.end();it++) cout<<*it; cout<<endl; } else cout<<"unsolvable"<<endl; } return 0; }
原文地址:https://www.cnblogs.com/175426-hzau-zxjc-con/p/10039810.html