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
超时:
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class A
{
static int c;
static int b[]={-1,1,-3,3};
static int tar=123456789;
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
HashSet set=new HashSet<>();
while(sc.hasNext())
{
String s;
int index=0;
int org=0;;
for(int i=0;i<9;i++)
{
s=sc.next();
if(!s.equals("x"))
{
org=org*10+Integer.valueOf(s);
}
else
{
org=org*10+9;
index=i;
}
}
Queue queue=new LinkedList<>();
queue.add(new state(org,' ',index,null));
set.add(org);
boolean sign=true;
state ans=null;
while(!queue.isEmpty()&&sign)
{
state t=queue.poll();
int a[]=getArray(t.s);
for(int i=0;i<4;i++)
{
if(!check(t.x,i))
continue;
int p=t.x+b[i];
int temp=a[t.x];
a[t.x]=a[p];
a[p]=temp;
int m=getS(a);
if(m==tar)
{
sign=false;
ans=new state(m,get(i),p,t);
break;
}
else
if(!set.contains(m))
{
queue.add(new state(m,get(i),p,t));
set.add(m);
}
a[p]=a[t.x];
a[t.x]=temp;
}
}
if(sign)
System.out.println("unsolvable");
else
{
Stackstack=new Stack<>();
while(ans!=null)
{
stack.add(ans);
ans=ans.last;
}
stack.pop();
while(!stack.isEmpty())
System.out.print(stack.pop().c);
System.out.println();
}
set.clear();
}
}
static int[] getArray(int s)
{
int a[]=new int[9];
int g=100000000;
for(int i=0;i<9;i++)
{
a[i]=s/g;
s%=g;
g/=10;
}
return a;
}
static int getS(int[] a)
{
int s=0;
for(int i=0;i<9;i++)
{
s=s*10+a[i];
}
return s;
}
static char get(int x)
{
switch (x)
{
case 0: return 'l';
case 1: return 'r';
case 2: return 'u';
case 3: return 'd';
}
return 0;
}
static boolean check(int x,int t)
{
if(t==0)
{
if(x==0||x==3||x==6)
return false;
}
else
if(t==1)
{
if(x==2||x==5||x==8)
return false;
}
else
if(t==2)
{
if(x==0||x==1||x==2)
return false;
}
else if(x==6||x==7||x==8)
return false;
return true;
}
}
class state
{
int s;
char c;
state last;
int x;
state(int s,char c,int x ,state last)
{
this.s=s;
this.c=c;
this.last=last;
this.x=x;
}
}