Eight
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21718   Accepted: 9611   Special Judge

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

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.

Sample Input

 2  3  4  1  5  x  7  6  8 

Sample Output

ullddrurdllurdruldr
/*
* 感言:广搜,题不是太标准,搜索方向顺序不同,结果不同,
* 不过可以AC,但是有可能会超时
*
*/
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner; public class Main{
static Map<String,Integer> map=new HashMap<String, Integer>();
static Queue<BFS> q=new ArrayDeque<BFS>();
static String end="12345678x";
static char g[]=new char[]{'u','l','d','r'};
static int d[][]=new int[][]{{-1,0},{0,-1},{1,0},{0,1}};//方向不同结果不同
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String s[][]=new String[3][3];
while(input.hasNext()){
map.clear();
q.clear();
BFS text=new BFS();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
s[i][j]=input.next();
if(s[i][j].equals("x")){
text.setX(i);
text.setY(j);
}
}
}
text.setS(s);
map.put(text.getKey(), 0);
q.add(text);
System.out.println(bfs(q)); }
}
private static String bfs(Queue<BFS> q2) {
while(!q2.isEmpty()){
BFS text=q2.poll();
if(end.equals(text.getKey())){
return text.getPath().toString();
}
for(int i=0;i<4;i++){
int x=text.getX()+d[i][0];
int y=text.getY()+d[i][1];
if(x<0||x>=3||y<0||y>=3)
continue;
String f[][]=text.copys();
f[text.getX()][text.getY()]=f[x][y];
f[x][y]="x";
BFS st=new BFS();
st.setX(x);
st.setY(y);
st.setS(f);
st.getPath().append(text.getPath()).append(g[i]);
String key=st.getKey();
if(!map.containsKey(key)){
q2.add(st);
map.put(key, 0);
}
}
}
return "unsolvable";
}
} class BFS{
private Integer x,y;
private String s[][];
private StringBuilder path=new StringBuilder();
public String[][] copys(){
String c[][]=new String[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
c[i][j]=s[i][j];
}
return c;
}
public String getKey(){
StringBuilder a=new StringBuilder();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
a.append(s[i][j]);
}
}
return a.toString();
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String[][] getS() {
return s;
}
public void setS(String[][] s) {
this.s = s;
}
public StringBuilder getPath() {
return path;
}
public void setPath(StringBuilder path) {
this.path = path;
}
}

最新文章

  1. Microsoft.Office.Interop.Excel 程序集引用 ,Microsoft.Office.Interop.Excel.ApplicationClass 无法嵌入互操作类型
  2. 三步搞定ISO/GHO安装系统 - imsoft.cnblogs
  3. Qt之图形(Source和Dest相互覆盖的取舍,真的很方便)
  4. [HIHO1318]非法二进制(动态规划)
  5. 使用DOM进行xml文档的crud(增删改查)操作&lt;操作详解&gt;
  6. android程序中使用命令行及获得命令行执行后的内容
  7. C# 连接 SQLServer 及操作
  8. declare 命令
  9. sort函数的用法与实验
  10. POJ 1486二分图的必要边
  11. 【填坑纪事】一次用System.nanoTime()填坑System.currentTimeMills()的实例记录
  12. 【vue】渲染大量数据时性能优化
  13. docker简单介绍----Dockerfile命令
  14. 手把手带你使用JS-SDK自定义微信分享效果
  15. 设计模式理解(八)结构型——装饰者模式(记得加上UML图 --- 未完)
  16. html页面导出为excel表格
  17. java通过传入的日期,获取所在周的周一至周日
  18. Linux 流量控制总结(TC)
  19. [Windows Azure] Managing SQL Database using SQL Server Management Studio
  20. 15.04.14-登录之后刷新AntiForgeryToken

热门文章

  1. 确实,k8s的时代,ingress负载用traefik比nginx方便啊
  2. 邂逅Sass和Compass之Sass篇
  3. 【转载】Scroller源码解析
  4. 在Eclipse调试Weblogic上的web项目
  5. [Lydsy1805月赛] quailty 算法
  6. 【随机化】Petrozavodsk Summer Training Camp 2016 Day 5: Petr Mitrichev Contest 14, Saturday, August 27, 2016 Problem I. Vier
  7. lightoj 1244 - Tiles 状态DP
  8. 进阶的Redis之哈希分片原理与集群实战
  9. Erlang学习记录(三)——表达式大集合
  10. Activity(活动)生命周期(2)--活动状态