Poj 1077 eight (one-way search)

Source: Internet
Author: User
Eight
Time limit:1000 ms   Memory limit:65536 K
Total submissions:18387   Accepted:8182   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 got 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:
 
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 4 1 2 3 4 4 4
5 6 7 8 5 6 7 8 5 6 6 7 8 5 6 7 8
9x10 12 9 10x12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14x15 14
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 extends 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 3x4 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 shoshould include no spaces and start at the beginning of the line.

Sample Input

 
2 3 4 1 5x7 6 8

Sample output

 
Ullddrurdllurdruldr

Source

Write it in various methods in south central USA 1998. Write a new Code :
 /*  Poj 1077 eight one-way search, from the forward start to find the target point Kanto expand as the hash value AC g ++ 8876 K 96 Ms */  # Include <Stdio. h> # Include < String . H> # Include <Iostream> # Include < String > Using   Namespace  STD;  Const   Int Maxn = 362881 ; // 9! = 362880  Struct  Node {  Int S [ 9  ];  Int Pre; //  Record previous Node      Int Dir; //  Record the direction from the previous node to this node  } Que [maxn];  Bool  Hash [maxn];  Int Path [maxn];  Int FAC [] = { 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 }; //  Kangtuo //  0! 1! 2! 3! 4! 5! 6! 7! 8! 9!  Int Cantor ( Int * S ){  Int Sum = 0  ;  For ( Int I = 0 ; I < 9 ; I ++ ){  Int Num =0  ;  For ( Int J = I + 1 ; J < 9 ; J ++ )  If (S [J] < S [I]) num ++ ; Sum + = (Num * FAC [ 9 -I- 1  ]);}  Return SUM ;}  Int Move [ 4 ] [ 2 ] = {{- 1 , 0 },{ 1 , 0 },{ 0 ,- 1 },{ 0 , 1 }}; //  U, D, L, R  Void Output ( Int  Len ){  For ( Int I = len- 1 ; I> = 0 ; I -- ){  If (Path [I] = 0 ) Printf ( "  U  "  );  Else   If (Path [I] =1 ) Printf ( "  D  "  );  Else   If (Path [I] = 2 ) Printf ( "  L  "  );  Else   If (Path [I] = 3 ) Printf ( " R  "  );} Printf (  "  \ N  "  );}  Void  BFS (){  Int Front =- 1 , Rear = 0  ; Que [  0 ]. Pre =- 1  ; Que [ 0 ]. Dir =- 1  ; Memset (hash,  False , Sizeof  (Hash); hash [Cantor (que [  0 ]. S)] = True  ;  If (Cantor (que [ 0 ]. S) = 0 ) {Output ( 0 ); Return  ;} While (Front < Rear) {front ++ ;  Int  TMP;  For (TMP = 0 ; TMP < 9 ; TMP ++ )  If (Que [Front]. s [TMP] = 9  )  Break  ; Int X = tmp/ 3  ;  Int Y = TMP % 3  ;  For ( Int I = 0 ; I < 4 ; I ++ ){  Int Tx = x + move [I] [ 0  ];  Int Ty = Y + move [I] [ 1  ];  If (TX < 0 | TX> 2 | Ty < 0 | Ty> 2 ) Continue  ; Que [rear + 1 ] = Que [Front]; que [rear + 1 ]. Pre = Front; que [rear +1 ]. Dir = I; que [rear + 1 ]. S [TMP] = que [rear + 1 ]. S [TX * 3 + Ty]; que [rear + 1 ]. S [TX * 3 + Ty] = 9  ;  Int Now = Cantor (que [rear + 1  ]. S ); If (Now = 0 ) //  Reach target  {  Int Len = 0  ;  Int T = rear + 1  ;  While (Que [T]. Pre! =- 1  ) {Path [Len ++] =Que [T]. dir; t = Que [T]. Pre;} output (LEN );  Return  ;}  If (! Hash [now]) {rear ++ ; Hash [now] = True  ;}}}}  Int  Main (){  // Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      Char STR [ 10  ];  While (Scanf ( "  % S  " , & Str )! = EOF ){  If (STR [ 0 ] = '  X ' ) Que [ 0 ]. S [ 0 ] = 9  ;  Else Que [ 0 ]. S [ 0 ] = STR [ 0 ]- '  0  '  ;  For ( Int I =1 ; I < 9 ; I ++ ) {Scanf (  "  % S  " ,& Str );  If (STR [ 0 ] = '  X  ' ) Que [ 0 ]. S [I] = 9  ; Else Que [ 0 ]. S [I] = STR [ 0 ]- '  0  '  ;} BFS ();}  Return   0  ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.