Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28459 Accepted Submission(s): 7557
Special Judge
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.
1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
ullddrurdllurdruldr
这里贴上代码,暂时只有一种,单向BFS+康托打表,我认为还是比较好理解的:
import java.util.Scanner;
import java.util.LinkedList;
public class Main {
static int[] fac={1,1,2,6,24,120,720,5040,40320,362880};
static NodeWay[] nn=new NodeWay[363000];
static int[][] dir={{0,1},{1,0},{-1,0},{0,-1}};
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner cin=new Scanner(System.in);
for(int i=0;i<nn.length;i++)
{
nn[i]=new NodeWay();
nn[i].Father=-1;
}
bfs();
while(cin.hasNext())
{
int[] cc=new int[9];
int count=0;
String str=cin.nextLine();
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==' ')continue;
if(str.charAt(i)=='x'){
cc[count++]=0;
}else{
cc[count++]=Integer.valueOf(str.charAt(i)+"");
}
}
int cantorr=cantor(cc,9);
if(nn[cantorr].Father==-1)System.out.println("unsolvable");
else{
while(nn[cantorr].Father!=0)
{
System.out.print(nn[cantorr].Way);
cantorr=nn[cantorr].Father;
}
System.out.println();
}
}
}
static int cantor(int[] number,int n)
{
int result=0;
for(int i=0;i<n;i++)
{
int count=0;
for(int j=i+1;j<n;j++)
{
if(number[i]>number[j])count++;
}
result+=count*fac[n-i-1];
}
return result+1;
}
static void bfs() //从123456780的末状态开始往之前的状态搜,搜出一种不同的就给他存起来
{
Node start=new Node();
start.CantorValue=46234;
for(int i=0;i<9;i++)
{
start.map[i]=(i+1)%9;
}
start.loc=8;
nn[46234].Father=0;
int row;
int col;
LinkedList<Node> queue=new LinkedList<Node>();
queue.add(start);
while(!queue.isEmpty())
{
Node node=queue.poll();
for(int i=0;i<4;i++)
{
row=node.loc/3+dir[i][0];
col=node.loc%3+dir[i][1];
if(row<0||col<0||row>=3||col>=3)continue;
Node work=new Node();
work.CantorValue=node.CantorValue;
work.loc=node.loc;
for(int k=0;k<9;k++){
work.map[k]=node.map[k];
}
work.loc=row*3+col;
int temp=0;
temp=work.map[node.loc];
work.map[node.loc]=work.map[work.loc];
work.map[work.loc]=temp;
work.CantorValue=cantor(work.map,9);
if(nn[work.CantorValue].Father==-1)
{
nn[work.CantorValue].Father=node.CantorValue;
if(i==0)nn[work.CantorValue].Way='l';
if(i==1)nn[work.CantorValue].Way='u';
if(i==2)nn[work.CantorValue].Way='d';
if(i==3)nn[work.CantorValue].Way='r';
queue.offer(work);
}
}
}
}
}
class NodeWay
{
char Way;
int Father;
NodeWay(){}
}
class Node
{
int CantorValue; //康托值
int[] map=new int[9]; //当前状态。用一维数组表示。
int loc; //标记出x的位置,方便操作
Node(){}
}
1092



被折叠的 条评论
为什么被折叠?



