Eight_pku_1077(广搜).java
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 21718 | Accepted: 9611 | Special Judge |
Description
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
1 2 3 x 4 6 7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
Output
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;
}
}
最新文章
- Microsoft.Office.Interop.Excel 程序集引用 ,Microsoft.Office.Interop.Excel.ApplicationClass 无法嵌入互操作类型
- 三步搞定ISO/GHO安装系统 - imsoft.cnblogs
- Qt之图形(Source和Dest相互覆盖的取舍,真的很方便)
- [HIHO1318]非法二进制(动态规划)
- 使用DOM进行xml文档的crud(增删改查)操作<;操作详解>;
- android程序中使用命令行及获得命令行执行后的内容
- C# 连接 SQLServer 及操作
- declare 命令
- sort函数的用法与实验
- POJ 1486二分图的必要边
- 【填坑纪事】一次用System.nanoTime()填坑System.currentTimeMills()的实例记录
- 【vue】渲染大量数据时性能优化
- docker简单介绍----Dockerfile命令
- 手把手带你使用JS-SDK自定义微信分享效果
- 设计模式理解(八)结构型——装饰者模式(记得加上UML图 --- 未完)
- html页面导出为excel表格
- java通过传入的日期,获取所在周的周一至周日
- Linux 流量控制总结(TC)
- [Windows Azure] Managing SQL Database using SQL Server Management Studio
- 15.04.14-登录之后刷新AntiForgeryToken
热门文章
- 确实,k8s的时代,ingress负载用traefik比nginx方便啊
- 邂逅Sass和Compass之Sass篇
- 【转载】Scroller源码解析
- 在Eclipse调试Weblogic上的web项目
- [Lydsy1805月赛] quailty 算法
- 【随机化】Petrozavodsk Summer Training Camp 2016 Day 5: Petr Mitrichev Contest 14, Saturday, August 27, 2016 Problem I. Vier
- lightoj 1244 - Tiles 状态DP
- 进阶的Redis之哈希分片原理与集群实战
- Erlang学习记录(三)——表达式大集合
- Activity(活动)生命周期(2)--活动状态