题目链接
康托展开与逆使用
题目思路演化过程
题目代码演化过程
感谢[kuangbin]的专题,太棒了,学到了许多
Problem 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:
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, 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
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. Do not print a blank line between cases.
Sample Input
2 3 4 1 5 x 7 6 8
大致题意
就是给你一个3x3的矩形,x是字母,问至少需要几步可以移动到顺序的状态
其实就是和我们小时候的3*3拼图,x是空白那格子,问最少需要几步
具体思路
多组数据其实很要命,一道bfs题从此走上了不归路,从最初的任意解,到后面好多人疯狂追寻最短路,具体的思想其实也很简单,记录路径的方式其实和简单搜索专题里的迷宫大致相同,记录动作和上一步,用回溯的方式记忆,但是在这里我们不难看出,九个格子,全排列,一开始个人用了过去以往的bfs记忆方式,即建立一个矩阵,在这里九个数,需要开拓九维矩阵。。。哦哄,头铁的我从不看时间和内存限制(毕竟我不会算)直接交一发,爆内存了,于是前往百度拜师,了解到了康托展开,利用康托展开将所有的情况与排列的矩阵利用离散映射压缩成一个36W左右的数组(具体详见顶部的链接),就这样,大模拟写了一下午,改了好久bug,终于AC了,康托牛逼
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int import[10];
int factorial[10];
int flags[400000]={0};
int change[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
char dic[10]={'d','u','r','l'};
struct Node//记录康托拓展s和坐标xy
{
int s,xy;
Node(){}
Node(int S,int XY){s=S,xy=XY;}
};
struct Path//记录这个图的上一步是什么
{
int from,opt;
}p[400000];
int getnum()//图转换为康托函数
{
int sum=0;
for (int i=0;i<=8;i++)
{
for (int j=i+1;j<=8;j++)
{
if (import[i]>import[j])
{
sum=sum+factorial[8-i];
}
}
}
return sum;
}
void getstr(int v)//康托函数转换为图
{
int tmp[10],flag[10];
memset(flag,0,sizeof(flag));
for (int i=0;i<=8;i++)
{
tmp[i]=v/factorial[8-i];
v=v%factorial[8-i];
}
for (int i=0;i<=8;i++)
{
int num=0;
for (int j=0;j<=8;j++)
{
if (flag[j]==0)
{
num++;
}
if (num==tmp[i]+1)
{
import[i]=j+1;
flag[j]=1;
break;
}
}
}
}
void Swap(int x,int y)//图的移动与交换
{
int d;
d=import[x];
import[x]=import[y];
import[y]=d;
}
void bfs()
{
queue<Node>dui;
dui.push(Node(0,8));
flags[0]=1;
p[0].from=-1;
p[0].opt=-1;
while (!dui.empty())
{
Node now=dui.front();
dui.pop();
int x=now.xy/3;
int y=now.xy%3;
getstr(now.s);
for (int i=0;i<4;i++)
{
int xx=x+change[i][0];
int yy=y+change[i][1];
if (!(xx>=0&&yy>=0&&xx<3&&yy<3))
{
continue;
}
int newxy=3*xx+yy;
Swap(newxy,now.xy);
int newnum=getnum();
if (flags[newnum]==1)
{
Swap(newxy,now.xy);
continue;
}
p[newnum].from=now.s;
p[newnum].opt=i;
flags[newnum]=1;
dui.push(Node(newnum,newxy));
Swap(newxy,now.xy);
}
}
}
int main()
{
factorial[0]=1;
for (int i=1;i<=8;i++)
{
factorial[i]=factorial[i-1]*i;
}
bfs();
char c;
while (scanf("%c%*c",&c)!=EOF)
{
if (c=='x')
{
import[0]=9;
}
else
{
import[0]=(c-'0')+0;
}
for (int i=1; i<9; i++)
{
scanf("%c%*c",&c);
if (c=='x')
{
import[i]=9;
}
else
{
import[i]=(c-'0')+0;
}
}
int arrange=getnum();
if (flags[arrange]==0)
{
printf("unsolvable\n");
}else
{
while (1)
{
if (p[arrange].from==-1)
{
break;
}
printf("%c",dic[p[arrange].opt]);
arrange=p[arrange].from;
}
printf("\n");
}
}
return 0;
}