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

Eight_pku_1077(广搜).java的更多相关文章

  1. Eleven puzzle_hdu_3095(双向广搜).java

    Eleven puzzle Time Limit: 20000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  3. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  4. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  5. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. 广搜 poj3278 poj1426 poj3126

    Catch That Cow Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Ja ...

  7. HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. vue之v-text渲染多值

    其原理,是利用vue里的computed计算属性来做. 请看代码: <div id='app'> <div v-text="newUsers"></d ...

  2. 3.spark streaming Job 架构和容错解析

    一.Spark streaming Job 架构 SparkStreaming框架会自动启动Job并每隔BatchDuration时间会自动触发Job的调用. Spark Streaming的Job ...

  3. [thinkphp] 隐藏后台地址

    转自 http://document.thinkphp.cn/manual_3_2.html#module_deploy 如果不希望用户直接访问某个模块,可以设置模块映射(对后台的保护会比较实用). ...

  4. centos xampp 隐藏phpmyadmin地址

    /opt/lampp/etc/extra/httpd-xampp.conf Alias /phpmyadmin "/opt/xampp/phpMyAdmin/" 改为 Alias ...

  5. HDU 6464.免费送气球-动态开点-权值线段树(序列中第first小至第second小的数值之和)(感觉就是只有一个状态的主席树) (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)

    免费送气球 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. JavaWeb中常见的乱码处理(亲测)

    常见编码方式: ISO-8859-1  西欧码 GB2312  简体中文码 GBK   大五码 UTF-8 全球码(推荐) 1.页面(HTML,JSP,Servlet) <%@ page lan ...

  7. AutoHotKey 常用脚本

    ; IMPORTANT INFO ABOUT GETTING STARTED: Lines that start with a ; semicolon, such as this one, are c ...

  8. Flask实战第37天:服务器权限验证

    完成服务器权限验证之前,我们先如下页面先补上 帖子管理 {% extends 'cms/cms_base.html' %} {% block title %} 帖子管理-CMS管理系统 {% endb ...

  9. [BZOJ1975][SDOI2010]魔法猪学院(k短路,A*)

    1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2748  Solved: 883[Submit][Statu ...

  10. POJ1704 Georgia and Bob 博弈论 尼姆博弈 阶梯博弈

    http://poj.org/problem?id=1704 我并不知道阶梯博弈是什么玩意儿,但是这道题的所有题解博客都写了这个标签,所以我也写了,百度了一下,大概是一种和这道题类似的能转换为尼姆博弈 ...