Sicily 1150 simple cube queue Solution

Source: Internet
Author: User
1150. Simple magic board Constraints

Time Limit: 1 secs, memory limit: 32 MB, special judge

Description

 

The magic board consists of eight blocks of the same size, respectively coated with different colors, represented by 1 to 8 numbers.

Its initial status is

1 2 3 4

8 7 6 5

Three basic operations can be performed on the magic board:

Operation A (SWAP between upper and lower rows ):

8 7 6 5

1 2 3 4

Operation B (one row to the right each time ):

4 1 2 3

5 8 7 6

C operation (four small pieces in the middle are converted clockwise ):

1 7 2 4

8 6 3 5

With the above three basic operations, you can replace any State with another State.

 

Input

 

The input includes multiple magic boards to be solved. Each magic board is described in three lines.

The number of steps N in the first line (an integer not greater than 10) indicates the maximum number of allowed steps.

The second and third lines indicate the target status. The color ranges from 1 to 8 according to the shape of the magic board.

When N is equal to-1, the input ends.

 

Output

 

Output a line for each magic board to be solved.

The first is an integer m, indicating the number of steps you need to find the answer. After a few spaces, start from step 1 and perform step m operations (each step is A, B, or C) in sequence. There is no space between two adjacent operations.

Note: if it cannot be reached, M outputs-1.

 

Sample Input
45 8 7 64 1 2 338 7 6 51 2 3 4-1
Sample output
2 Ab1 a score: If M exceeds N or the given operation is incorrect, the score cannot be obtained.
Problem Source

Zsuacm team member

 

# Include <iostream> # include <vector> # include <stack> using namespace STD; // define a cube struct typedef struct MF {int up; // int down of the cube's upstream digit; // char op of the cube's downstream digit; // obtain the Cube's int pre by this operation; // record the Cube's previous cube, that is, The subscripts of the parent node in the triplicate tree in the queue are used to trace the transformations that have taken the shortest result to the cube. This design is critical} MF; // compare whether the cube to be inserted already has bool CMP (vector <MF> & MFs, MF, char op, int pre) in the queue {for (INT I = 0; I <MFs. size (); I ++) {If (MFs [I]. up = mf. up & MFs [I]. down = mf. down ){/ /If the cube to be inserted already exists in the queue, the cube is not inserted, that is, the return false;} // If the Cube does not exist in the queue, insert MF. pre = pre; Mf. OP = op; MFs. push_back (MF); Return true;} // There are three operations in total, therefore, for the trigger tree // operate a 1234 8765-> 8765 1234, return the cube char OPA (MF, int pre, MF & result) after the operation through result reference) {int M = mf. up; int n = mf. down; Mf. up = N; Mf. down = m; Result = mf; result. pre = pre; result. OP = 'a'; return 'a';} // operation B 1234 8765-> 4123 5876 char OPB (MF, int pre, MF & Result) {Int up_first = (MF. up % 10) * 1000; int down_first = (MF. down % 10) * 1000; int up_last = (MF. up-(MF. up/1000) * 1000)-(MF. up-(MF. up/1000) * 1000)/100) * 100)/10; int down_last = (MF. down-(MF. down/1000) * 1000)-(MF. down-(MF. down/1000) * 1000)/100) * 100)/10; Mf. up = up_first + (MF. up/1000) * 100 + (MF. up-(MF. up/1000) * 1000)/100) * 10 + up_last; Mf. down = down _ First + (MF. down/1000) * 100 + (MF. down-(MF. down/1000) * 1000)/100) * 10 + down_last; Result = mf; result. pre = pre; result. OP = 'B'; return 'B';} // operation C 1234 5678-> 1624 5738 char OPC (MF, int pre, MF & result) {int I1 = (MF. up-(MF. up/1000) * 1000)/100; int I2 = (MF. up-(MF. up/1000) x 1000)-I1 * 100)/10; int J1 = (MF. down-(MF. down/1000) * 1000)/100; int J2 = (MF. down-(MF. Down/1000) * 1000)-J1 * 100)/10; Mf. up = (MF. up/1000) * 1000 + J1 * 100 + I1 * 10 + Mf. up % 10; Mf. down = (MF. down/1000) * 1000 + J2 * 100 + I2 * 10 + Mf. down % 10; Result = mf; result. pre = pre; result. OP = 'C'; return 'C';} int main () {int Max; while (CIN> MAX & Max! =-1) {int find_length = 0; int target [8]; int value; For (INT I = 0; I <8; I ++) {CIN> value; target [I] = value;} vector <MF> MFs; stack <char> OPS; int fp = 0, Rp = 0; // FP is the header pointer, pointing to the node currently being processed, RP is the tail pointer of the queue, pointing to the node bool success = false; int up = target [0] * 1000 + target [1] * 100 + target [2] * 10 + target [3]; // The upstream int down of the target cube = target [7] + target [6] * 10 + target [5] * 100 + target [4] * 1000; // downstream MF of the target cube; Mf. up = 1234; Mf. down = 8765; Mf. OP = ''; Mf. pre =-1; MFs. push_back (MF); // if not found, the while (MFs [FP] is searched cyclically. up! = Up | MFs [FP]. Down! = Down) {// perform operations on the cube. the header pointer executes the cube that is being operated. The final pointer executes the operation and then enters the cube MF result of the queue; char A = OPA (MFs [FP], FP, result); // check whether the target State is if (result. up = up & result. down = down) {// if the target cube is found, the Operation Sequence while (result. pre! =-1) {ops. push (result. OP); find_length ++; Result = MFs [result. pre]; // backtracking} success = true; break;} else if (CMP (MFs, result, A, FP) {// If the cube is not in the queue, this cube enters the team RP ++; // tail pointer increments} Char B = OPB (MFs [FP], FP, result); If (result. up = up & result. down = down) {// if the target cube is found, the Operation Sequence while (result. pre! =-1) {ops. push (result. OP); find_length ++; Result = MFs [result. pre]; // backtracking} success = true; break;} else if (CMP (MFs, result, B, FP) {RP ++ ;} char c = OPC (MFs [FP], FP, result); If (result. up = up & result. down = down) {// if the target cube is found, the Operation Sequence while (result. pre! =-1) {ops. push (result. OP); find_length ++; Result = MFs [result. pre]; // backtracking} success = true; break;} else if (CMP (MFs, result, C, FP) {RP ++;} FP ++; // The increment of the header pointer} // if it cannot be found within the specified step, the output is-1 If (find_length> MAX) {cout <-1 <Endl ;} else {cout <find_length <""; while (! Ops. empty () {// locate the cout process in which the output changes from the initial state to the target State <ops. top (); ops. pop () ;}cout <Endl ;}} return 0 ;}

  

Sicily 1150 simple cube queue Solution

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.