Hdu1430 (bfs)
Magic BoardTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 2398 Accepted Submission (s): 504
Shortly after Problem Description swept the globe, Mr. Rubik invented its simplified version, the magic board. The magic board consists of eight blocks of the same size, each of which has different colors and can be represented by numbers 1-8. At any time, the status of the magic board can be represented by the color sequence of the Square: from the upper left corner of the magic board, write the color code of each square in clockwise order, the sequence of digits indicates the status of the magic board. For example, the sequence (,) indicates that the magic board status is:
1 2 3 4
8 7 6 5
Three different operations can be applied to the magic board:
A: swap the upper and lower lines, for example, change to status 87654321.
B: Shifts one cell to the right of each row at the same time, for example, 41236785.
C: four boxes in the middle rotate one grid clockwise. For example, the variable is changed to 17245368.
For the initial and target statuses of the magic board, please give the transformation steps from the initial state to the smallest number of changes in the object state. If there are multiple conversion schemes, take the smallest Lexicographic Order.
Each group of Input test data includes two rows, representing the initial and object states of the magic board.
Output converts the Output of each group of test data to meet the requirements of the question.
Sample Input
12345678172453681234567882754631
Sample Output
CAC
Author LL
Source ACM summer training team exercise session (3)
Analysis: the question itself is difficult, mainly to mark the trouble. This requires a new knowledge. You can click here to know it, And then pre-process it, for each case, we can see that "12345678" is converted to another situation. This value is bfs once, instead of bfs every time, which saves a lot of time.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include using namespace std; const double eps = 1e-6; const double pi = acos (-1.0); const int INF = 0x3f3f3f; const int MOD = 1000000007; # define ll long # define CL (a) memset (a, 0, sizeof (a) string s, t, dp [50005]; int vis [50005]; int func [11], pos [11]; struct node {int va; string str, ans ;}; int Hash (string & s) // expand Kang Tuo {int va = 0; for (int I = 0; I <7; I ++) {int cnt = 0; for (int j = I + 1; j <8; j ++) if (s [j]
= 0; I --) s [I + 1] = s [I]; s [0] = ff; for (int I = 6; I> = 4; I --) s [I + 1] = s [I]; s [4] = dd;} void goto3 (string & s) {char ff = s [1]; s [1] = s [5]; s [5] = s [6]; s [6] = s [2]; s [2] = ff ;} void bfs () {CL (vis); node now, next; queue
Q; now. str = s; now. ans =; now. va = Hash (s); vis [now. va] = 1; dp [now. va] =; q. push (now); while (! Q. empty () {now = q. front (); q. pop (); string t = now. str; goto1 (t); int k = Hash (t); if (! Vis [k]) {next. str = t; vis [k] = 1; next. ans = now. ans + 'a'; next. va = k; dp [k] = next. ans; q. push (next);} t = now. str; goto2 (t); k = Hash (t); if (! Vis [k]) {next. str = t; vis [k] = 1; next. ans = now. ans + 'B'; next. va = k; dp [k] = next. ans; q. push (next);} t = now. str; goto3 (t); k = Hash (t); if (! Vis [k]) {next. str = t; vis [k] = 1; next. ans = now. ans + 'C'; next. va = k; dp [k] = next. ans; q. push (next) ;}}int main () {func [0] = 1; for (int I = 1; I <= 9; I ++) func [I] = func [I-1] * I; s = 12345678; bfs (); while (cin> s> t) {swap (s [4], s [7]); swap (s [5], s [6]); swap (t [4], t [7]); swap (t [5], t [6]); for (int I = 0; I <8; I ++) pos [s [I]-'0'] = I + 1; for (int I = 0; I <8; I ++) // Replace the value with "13245678" into another string t [I] = pos [t [I]-'0']; int k = Hash (t); cout <