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:
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(); } }