pku 1077 Eight BFS
来源:互联网 发布:崩坏3矩阵探索buff最新 编辑:程序博客网 时间:2024/05/22 13:45
Eight
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 21718 Accepted: 9611 Special Judgehttp://poj.org/problem?id=1077
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:
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:
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.
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
is described by this list:
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
import java.util.ArrayDeque;import java.util.HashMap;import java.util.Map;import java.util.Queue;import java.util.Scanner;public class cppku1077 {private static Map<String,Integer> map = new HashMap<String,Integer>();private static Queue<Status> queue = new ArrayDeque<Status>();private static String endStatus = "12345678x";private static int [][] dirs = {{-1,0},{0,-1},{1,0},{0,1}};private static char[] path ={'u','l','d','r'};public static void main(String[] args) {Scanner cin = new Scanner(System.in);String [][] array = new String[3][3];while(cin.hasNext()){map.clear();queue.clear();Status start = new Status();for(int i=0;i<array.length;i++){for(int j=0;j<array[0].length;j++){array[i][j]=cin.next();if(array[i][j].equals("x")){start.setX(i);start.setY(j);}}}start.setDepth(0);start.setArray(array);map.put(start.getKey(), 0);queue.add(start);String result = bfs(queue);System.out.println(result);}}public static String bfs(Queue<Status> queue){while(!queue.isEmpty()){Status s= queue.poll();if(endStatus.equals(s.getKey())){return s.getPath().toString();}for(int i=0;i<dirs.length;i++){int x=s.getX()+dirs[i][0];int y =s.getY()+dirs[i][1];if(x<0 || x>=s.getArray().length ||y<0 || y>=s.getArray()[x].length){continue;}String [][] array =s.copyArray();array[s.getX()][s.getY()] = array[x][y];array[x][y] = "x";Status status = new Status();status.setArray(array);status.setX(x);status.setY(y);status.getPath().append(s.getPath()).append(path[i]);status.setDepth(s.getDepth()+1);String key =status.getKey();if(!map.containsKey(key)){queue.add(status);map.put(key, status.getDepth());}}}return endStatus;}}class Status {private Integer depth;private String [][] array;private Integer x;private Integer y;private StringBuilder path = new StringBuilder();public Integer getDepth() {return depth;}public void setDepth(Integer depth) {this.depth = depth;}public String[][] getArray() {return array;}public void setArray(String[][] array) {this.array = array;}public Integer getX() {return x;}public void setX(Integer x) {this.x = x;}public Integer getY() {return y;}public void setY(Integer y) {this.y = y;}public StringBuilder getPath() {return path;}public void setPath(StringBuilder path) {this.path = path;}public String[][] copyArray(){String[][] a =new String[array.length][array[0].length];for(int i=0;i<a.length;i++){for(int j=0;j<a[0].length;j++){a[i][j]=array[i][j];}}return a;}public String getKey(){StringBuilder sb =new StringBuilder();for(int i=0;i<array.length;i++){for(int j=0;j<array[0].length;j++){sb.append(array[i][j]);}}return sb.toString();}}
- pku 1077 Eight BFS
- PKU-1077 Eight (八数码单向BFS+HASH)
- PKU-1077 Eight (八数码之双向BFS)
- pku 1077 Eight 经典8数码问题 单向BFS + A* BFS + 双向BFS
- poj 1077 Eight (BFS)
- POJ 1077 Eight BFS -
- poj 1077 Eight(BFS + 康托判重)
- POJ 1077 Eight(BFS Hash)
- PKU-1077 Eight (八数码之IDA*)
- POJ 1077 Eight(BFS八数码问题)
- poj 1077 Eight (bfs A* IDA*)
- POJ 1077 Eight(BFS:输出路径)
- POJ 1077 Eight 八数码问题 BFS
- POJ 1077-Eight(BFS+优先队列)
- PKU-1077 Eight (八数码之A*算法)
- pku 1077 Eight【八数码、广搜、hash判重】
- POJ 1077 Eight(康拓展开 BFS 双向BFS)
- HDU 1043 && POJ 1077 Eight bfs || 双向bfs || A*搜索
- Unity 3D中的内存管理
- opcache安装及概念性了解
- IE6的window.location.href不跳转的问题
- 谈谈Objective-C的警告
- 把自动机用作 Key-Value 存储
- pku 1077 Eight BFS
- Redhat6.3安装matlab找不到libXP.so.6解决方法
- PythonChallenge 挑战之路 Level-31
- hdu4574Bombs(枚举+dfs)
- Uninstall Bitdefender Antivirus Free Edition with WindowsUninstaller.Org Removal Tips
- C#生成pfx用户数字证书
- URLRewriter
- visual studio 常用快捷键
- flex 弹出框Alert