Hdu 1043 8 digital Classic search problem BFS + MAP

Source: Internet
Author: User

Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 8747 Accepted Submission (s): 2387
Special Judge


Problem 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, several descriptions of configuration of the 8 puzzle. one 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. do not print a blank line between cases.

 


Sample Input
2 3 4 1 5x7 6 8


Sample Output
Ullddrurdllurdruldr


Source
South Central USA 1998 (Sepcial Judge Module By JGShining)
 


Recommend
JGShining
Ideas:
There are a total of 9 states that have passed through access with map! Status
Then, use 123456780 to push back all the statuses that can reach the final state.
Note:
Int dir [4] [2] = {-}, {}, {0,-1 }};
Char fx [] = {'D', 'U', 'R', 'L '};
-1 0: the corresponding direction is down.
X is reduced. x is horizontal. So it should be up.
However, reverse output also requires reverse output.
Because the output needs to be output from the final state 123456780 in reverse order. The output must be output from top to bottom, from top to left, and from right to left.
 
# Include <stdio. h>
# Include <string>
# Include <queue>
# Include <map>
Using namespace std;
Struct haha
{
Int pos;
String path;
String str;
Haha ()
{
Pos = 0;
}
} Q, temp;
Int dir [4] [2] = {,-, 0,-1}, pos;
String begin;
Char fx [4] = {'U', 'D', 'l', 'R '};
Map <string, struct haha> mp;
Void BFS ()
{
Int I;
Queue <struct haha> que;
Q. path = "";
Q. pos = pos;
Q. str = begin;
String mid;
Mp [begin]. pos = 1;
Que. push (q );
While (! Que. empty ())
{
Temp = que. front ();
Que. pop ();
For (I = 0; I <4; I ++)
{
Int x = temp. pos/3 + dir [I] [0];
Int y = temp. pos % 3 + dir [I] [1];
If (x <0 | x> 2 | y <0 | y> 2) continue;
Mid = temp. str;
Pos = temp. pos; // weight
If (I = 0)
{
Mid [pos + 3] = temp. str [pos];
Mid [pos] = temp. str [pos + 3];
Q. pos = pos + 3;
}
Else if (I = 1)
{
Mid [pos-3] = temp. str [pos];
Mid [pos] = temp. str [pos-3];
Q. pos = pos-3 ;//
}
Else if (I = 2)
{
Mid [pos + 1] = temp. str [pos];
Mid [pos] = temp. str [pos + 1];
Q. pos = pos + 1;
}
Else
{
Mid [pos-1] = temp. str [pos];
Mid [pos] = temp. str [pos-1];
Q. pos = pos-1;
}

If (mp [mid]. pos = 0)
{
Q. path = temp. path;
Q. path + = fx [I];
Q. str = mid;
Mp [mid]. path = q. path;
Mp [mid]. pos = 1;
Que. push (q );
}
}
}

}
Int main ()
{
Int I, cnt = 0;
Char s [2];
Begin = "12345678x ";
Pos = 8;
BFS ();
Begin = "";
While (scanf ("% s", s )! = EOF)
{
Cnt ++;
Begin + = s [0];
If (cnt = 9)
{
If (mp [begin]. pos = 0)
Printf ("unsolvable \ n ");
Else
{
For (I = mp [begin]. path. size ()-1; I> = 0; I --)
Printf ("% c", mp [begin]. path [I]);
Printf ("\ n ");
}
Begin = ""; cnt = 0;
}
}

Return 0;

Related Article

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.