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;
}
}
Eight_pku_1077(广搜).java的更多相关文章
- Eleven puzzle_hdu_3095(双向广搜).java
Eleven puzzle Time Limit: 20000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- nyoj 613 免费馅饼 广搜
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 广搜 poj3278 poj1426 poj3126
Catch That Cow Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Ja ...
- HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)
THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- Message Queue中的推与拉(转)
Message Queue的设计和实现(7)http://mp.weixin.qq.com/s/zQdDBAHu1UgJJzxH2eCHgQ 数据发送中的推与拉. 当MQ要把数据给消费者的时候,就涉及 ...
- 前端读者 | ES6知识点概述
本文来自 @羯瑞 整理 ES6,并不是一个新鲜的东西,ES7.ES8已经赶脚了.但是,东西不在于新,而在于总结. 变量的新定义 let 和 const 在ES6没有被普及时,我们会用的变量定义的方法是 ...
- 【剑指offer】面试题 9. 用两个栈实现队列
面试题 9. 用两个栈实现队列 题目描述 题目:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解答过程 import java.util.Stack; publ ...
- java之异常
package com.text.exception; class Test{ void add(int a,int b) throws Exception { int c; c=a/b; Syste ...
- (8)go 字符串
内建函数在 包中 1. len(str) 计算长度,中文占3个字符 2.字符串遍历,同时处理中文 package main import ( "fmt" ) func main() ...
- SVN版本控制软件
一.版本控制软件 1.为什么需要版本控制软件 问题:① 团队开发 ② 异地协作 ③ 版本回退 2.解决之道 SCM(Software Configuration Management):软件配置管理 ...
- python之面向对象编程二
类的成员 类的成员可以分为三大类:字段.方法.属性. 字段:普通字段.静态字段. 方法:普通方法.类方法.静态方法 属性:普通属性. 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多 ...
- NGUI EventDelagate事件委托
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BUttonCl ...
- 【C++】析构函数的作用和用法
一.定义1. 作用:对象消亡时,自动被调用,用来释放对象占用的空间2.特点: (1) 名字与类名相同 (2) 在前面需要加上"~" (3) 无参数,无返回值 (4) ...
- 中间件和auth模块
中间件 1.什么是中间件 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用,用 ...