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. sql中where和having的区别

    “Where” 是一个约束声明,使用Where来约束来自数据库的数据,Where是在结果返回之前起作用的,且Where中不能使用聚合函数. “Having”是一个过滤声明,是在查询返回结果集以后对查询 ...

  2. Linux下使用ssh远程登录服务器

    如果自己的服务器是在内网,想在外网通过ssh在自己的VPS服务器上远程登录自己的内网服务器,可以按照如下操作: 一.在自己的服务器上使用如下命令: #ssh -CfnNT -R 端口A:localho ...

  3. 五十一 常用第三方模块virtualenv

    在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目录下. 如果我们要同时开发多个应用程序,那这 ...

  4. python之md5模块

    python的md5模块使用非常简单,包括以下几个函数: md5.new([arg]) 返回一个md5对象,如果给出参数,则相当于调用了update(arg) md5.updte(arg) 用stri ...

  5. Codeforces 713A. Sonya and Queries

    题目链接:http://codeforces.com/problemset/problem/713/A 题意: Sonya 有一个可放置重复元素的集合 multiset, 初始状态为空, 现给予三种类 ...

  6. LibreOJ #139 树链剖分 [树链剖分,线段树]

    题目传送门 树链剖分 题目描述 这是一道模板题. 给定一棵 n 个节点的树,初始时该树的根为 1 号节点,每个节点有一个给定的权值.下面依次进行 m 个操作,操作分为如下五种类型: 换根:将一个指定的 ...

  7. python 理解高阶函数

    高阶函数 高阶函数英文叫Higher-order function.什么是高阶函数? 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用abs(): >>> a ...

  8. 使用Spring JdbcTemplate实现CLOB和BLOB的存取

    概述 本文讲述通过Spring的JdbcTemplate来读写数据库大字段的实现方案,在一位网友的一篇博客的基础上,查看api文档整理而成. 写实现 JdbcTemplate jdbcTemplate ...

  9. Loj#6433「PKUSC2018」最大前缀和(状态压缩DP)

    题面 Loj 题解 先转化题意,其实这题在乘了\(n!\)以后就变成了全排列中的最大前缀和的和(有点拗口).\(n\leq20\),考虑状压\(DP\) 考虑一个最大前缀和\(\sum\limits_ ...

  10. 设计BBS

    功能分析: 1 登陆功能(基于ajax,图片验证码) 2 注册功能(基于ajax,基于forms验证) 3 博客首页 4 个人站点 5 文章详情 6 点赞,点踩 7 评论 --根评论 --子评论 8 ...