poj-1077-Eight

来源:互联网 发布:qq登录代理服务器 mac 编辑:程序博客网 时间:2024/06/05 16:24
Eight
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 21713 Accepted: 9607 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

Source

South Central USA 1998
import java.util.ArrayDeque;import java.util.HashMap;import java.util.Map;import java.util.Queue;import java.util.Scanner;public class Main {/** * @param args */static Map<String,Integer> map=new HashMap<String,Integer>();static Queue<Status> queue=new ArrayDeque<Status>();static String endStatus="12345678x";static int[][] dirs={{-1,0},{0,-1},{1,0},{0,1}};static char[] path={'u','l','d','r'};public static void main(String[] args) {// TODO Auto-generated method stubScanner input=new Scanner(System.in);String[][] array = new String[3][3];while(input.hasNext()){map.clear();queue.clear();Status start=new Status();for(int i=0;i<array.length;i++)for(int j=0;j<array[i].length;j++){array[i][j]=input.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);}}//mainprivate static String bfs(Queue<Status> queue) {// TODO Auto-generated method stubwhile(!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 "unsolvable";}}class Status{Integer depth;String[][] array;Integer x;Integer y;StringBuilder path=new StringBuilder();//depthpublic 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;}//xpublic Integer getX() {return x;}public void setX(Integer x) {this.x = x;}//ypublic Integer getY() {return y;}public void setY(Integer y) {this.y = y;}//pathpublic 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[i].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[i].length;++j)sb.append(array[i][j]);return sb.toString();}}