hdu 1043 /poj 1077 Eight(经典八数码问题,BFS+康托展开)

时间:2022-12-09 09:53:08

Eight

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20170    Accepted Submission(s): 5371
Special Judge


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: 
 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, 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
 

Sample Output
  
  
  
ullddrurdllurdruldr
 

Source
 
题意:经典八数码问题,移动x使得序列变成12345678_(_是空格),求最少移动次数的一个序列

思路:这个题hdu比poj的要紧一点,所以hdu能过的话poj一般就没问题。

用bfs去打表,一共只有9!(大概37w)个状态,不算多,记录从12345678_这个状态能够走到的所有的状态,并记录其父亲状态以及走的方向

然后我们每次输入序列只要一直找父亲,边输出路径,直到找到根结束即可。

方向就记反向的,方便我们找的时候直接输出。

记录一个序列,我们可以用hash,当然用普通hash是不行的,要用到康托展开hash,可以去看下我的前一篇博客或者百度百科,很好理解的东西。

不太会用A*...所以还没看懂A*的解法,真是菜啊=-=

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
struct Node
{
char step;
int father;
} node[380005];
struct Node1
{
int a[10];
int n,id;
};
int f[10],num[10];
int dir[4][2]= {-1,0,1,0,0,1,0,-1};
void init()
{
f[0]=1;
for(int i=1; i<=8; i++)
f[i]=f[i-1]*i;
for(int i=1; i<380000; i++)
node[i].father=-1;
node[0].father=0;
}
int cantor(int *a)
{
int ans=0;
for(int i=1; i<=9; i++)
{
int k=0;
for(int j=i+1; j<=9; j++)
if(a[i]>a[j])
k++;
ans+=k*f[9-i];
}
return ans;
}
void bfs()
{
queue<Node1>que;
Node1 q,next;
memcpy(q.a,num,sizeof(num));
q.n=9;
q.id=0;
que.push(q);
while(!que.empty())
{
Node1 now=que.front();
que.pop();
for(int i=0; i<4; i++)
{
int x= now.n%3==0?now.n/3+dir[i][0]:now.n/3+1+dir[i][0];
int y= now.n%3==0?dir[i][1]+3:now.n%3+dir[i][1];
if(x<1||x>3||y<1||y>3) continue;
next.n=(x-1)*3+y;
memcpy(next.a,now.a,sizeof(now.a));
swap(next.a[now.n],next.a[next.n]);
next.id=cantor(next.a);
if(node[next.id].father==-1)
{
node[next.id].father=now.id;
if(i==0) node[next.id].step='d';
else if(i==1) node[next.id].step='u';
else if(i==2) node[next.id].step='l';
else node[next.id].step='r';
que.push(next);
}
}
}
}
int main()
{
char op[100];
int ans[10];
init();
for(int i=1; i<=9; i++)
num[i]=i;
bfs();
while(gets(op)>0)
{
int t=0;
for(int i=0; i<strlen(op); i++)
{
if(op[i]=='x') ans[++t]=9;
else if(op[i]>='0'&&op[i]<='9') ans[++t]=op[i]-'0';
}
int id=cantor(ans);
if(node[id].father==-1)
{
printf("unsolvable\n");
continue;
}
while(id)
{
printf("%c",node[id].step);
id=node[id].father;
}
printf("\n");
}
return 0;
}