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.
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
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.
2 3 4 1 5 x 7 6 8
ullddrurdllurdruldr
这道题目是一道典型的搜索题目,什么搜索?自然是广搜。面对这一道题,其中最为复杂的就是如何储存?如何判重?你想到了吗?
面对如何储存的这一个问题,我选择了栈,因为它可以更好的帮助我储存。至于如何判重,我选择了先用一个一维数组储存,再运用以下方法:
若有5个数; 2 4 5 7 8
我们如何来进行判重呢?
首先我们需要一个 bool 数组来标记曾经产生过的值,避免重复产生。
那我们如何产生这样一个值呢?
这时,需要两个 for 循环来辅助,不光如此,我们还需要一个阶层的数组,以辅助产生。
第一个 for 枚举第一个数 i:1~n(从前往后),第二数枚举 j:i+1~n (从前往后),一旦遇到比第一个数小的就加上 [8-j]的阶层如此,我们就创造出了一个数所独有的自己的编码。至于为什么是 8-j 而不是 j,也是为了运算速度着想,不过,也可以用 j。
知道了这个方法,妈妈再也不用担心我的判重了(-__-),另外一个需要注意的是:如何判断是否越界?请细细理解下面这段代码:
if(push.where<0||push.where>8||(i%2&&push.where/3!=first.where/3))
push.where and first.where为新的位置编号与原来的位置编号。这里的判重终点是左右走(判断的行号)。
好啦,下面自己消化代码吧 -_-
【代码】
状态:AC
#include
#include
#include
using namespace std;
int Jc[15]={1,1,2,6,24,120,720,5040,40320,362880,3628800};
int Fx[4]={-3,-1,3,1};
char Sc[4]={'u','l','d','r'};
struct Queue
{
int num[15];
int Dad,F,where;
}a[600000];
Queue begin;
bool map[362885];
bool Dd(int n[])
{
for(int i=0;i<8;i++)
if(n[i]!=i+1)
return false;
return true;
}
bool Pc(int n[])
{
int lj=0;
for(int i=0;i<9;i++)
for(int j=i+1;j<9;j++)
if(n[j]8-i];
if(map[lj]) return true;
map[lj]=true;
return false;
}
int main()
{
int l=0,r=0;
char c=0;
begin.Dad=begin.F=-1;
for(int i=0;i<9;i++)
{
scanf("%c",&c);
if(c-'0'==-16)scanf("%c",&c);
if(c!='x')begin.num[i]=c-'0';
else begin.num[i]=0,begin.where=i;
}
//for(int i=0;i<9;i++)
//printf("%d ",begin.num[i]);
a[r++]=begin;
while(lfor(int i=0;i<4;i++)
{
Queue push=first;
push.Dad=l,push.F=i,push.where=Fx[i]+first.where;
if(push.where<0||push.where>8||(i%2&&push.where/3!=first.where/3)) continue;
swap(push.num[push.where],push.num[first.where]);
if(Pc(push.num)) continue;
a[r++]=push;
if(Dd(push.num))
{
int x=r-1;
vector<int> vec;
while(true)
{
vec.push_back(a[x].F);
x=a[x].Dad;
if(x==-1) break;
}
for(int j=vec.size()-2;j>=0;j--)
printf("%c",Sc[vec[j]]);
printf("\n");
return 0;
}
}
l++;
}
printf("NO SOLUTION\n");
return 0;
}