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
算法解析:八数码模板题.
/*
BFS+Cantor()模板 解决八数码,十六数码问题.
*/
//#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <deque>
#include <queue>
#include <string>
#include <cstring>
const int LEN = 362880;
using namespace std;
struct node{
int state[9];
int dis;
char line[1000];//记录状态.
};
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};//上下,右左四个方向
int visited[LEN] = {0};//每个状态对应的记录, 用于标记是否
int start[9];//开始状态
int goal[9]; //目标状态
long int factory[] = {1,1,2,6,24,120,720,5040,40320,362880};//Cantor()用到的常数==> 0!~9!
int Cantor(int str[],int n){//用康拓展开来判重
long result = 0;//str排在第几位
for(int i = 0; i < n; i++){
int counted = 0;
for(int j = i+1;j < n; j++){
if(str[i]>str[j]){
counted++;//当前未出现的元素排在第几位
}
}
result+=counted * factory[n-i-1];
}
if(!visited[result]){//没有被访问过
visited[result] = 1;
return 1;
}else{
return 0;
}
}
int bfs(node &desNode){
node head;//用于存放起始点
memcpy(head.state,start,sizeof(head.state)); // memcpy(void *dest, void *src, int count) 由src所指内存区域复制count个字节到dest所指内存区域。
head.dis = 0;
queue<node> q;
Cantor(head.state,9);//标记起点 已经被访问过.
q.push(head);//进队列
while(!q.empty()){
head = q.front();
q.pop();
if(memcmp(head.state,goal,sizeof(goal))==0){
memcpy(desNode.line,head.line,sizeof(head.line));
return head.dis;//到达目标状态,结束
}
int z;
for(z = 0;z < 9;z++){//找这个状态中元素0的位置
if(head.state[z]==0){
break;
}
}
//转换为二维,左上角是 (0,0)
int y = z %3;
int x = z / 3;
for(int i = 0; i < 4;i++){
int newX = x+dir[i][0];//元素0 转移后的新坐标
int newY = y + dir[i][1];
int nz = newX*3+newY;
if(newX>=0&&newX<3&&newY>=0&&newY<3){//判断是否越界
node newnode;
memcpy(&newnode,&head,sizeof(struct node));// 用head对newnode进行初始化
swap(newnode.state[z],newnode.state[nz]);// 把0移动到新的位置
newnode.dis++;
if(Cantor(newnode.state,9)){//判断是否被访问过
if(i == 0){//上
strcat(newnode.line,"u");
//newnode.line
}else if(i==1){//下
strcat(newnode.line,"d");
}else if(i==2){//左
strcat(newnode.line,"l");
}else{//右
strcat(newnode.line,"r");
}
q.push(newnode);//将其放入到队列中
}
//把0转移到新的位置.
}
}
}
return -1;//没找到
}
char ch;
int main()
{
node disNode;
for(int i = 0;i <9;i++){
cin>>ch;
if(ch!='x'){
start[i] = ch - '0';
}else{
start[i] = 0;
}
//cin>>start[i];
}
for(int i = 0;i < 9;i++){
goal[i] = i+1;
}
goal[8] = 0;
int num = bfs(disNode);
if(num!=-1){
//printf("%s\n",disNode.line);
cout<<disNode.line<<endl;
}else{
//printf("unsolvable\n");
cout<<"unsolvable"<<endl;
}
return 0;
}