Let's talk about:
In fact, this question is not very difficult, but complicated. First, the question is probably like this. At the beginning, there were several numbered boxes. Then there are a total of four operations: MoveAOntoBFirst, the boxes numbered A and B are returned to the initial position, and then a is placed on B. MoveAOverBReturn the initial position of the box on a and place a on the heap of the box where B is located. PileAOntoB, Return the initial position of the box on B, and then place a and the box above B. PileAOverBPut a and the box on it on the heap of B. The solution is actually very simple. Find the serial number of the heap where A and B are located and their position in the heap, and then follow the instructions step by step. I started to think that the details have been adjusted for a long time. I didn't expect it to be AC once, so happy = V =
Source code:
# Include <stdio. h> # include <string. h> # define maxn 30int stack [maxn] [maxn]; char stack_num [maxn]; void back_initial (INT, INT ); // return all the boxes in a certain box to the initial position int main () {int N, A, B, I, J, astack, bstack, APOs, and BPOs; // astack is the ID of the heap where A is located. APOs is Char S1 [5], S2 [5], where A is located. // freopen ("data ", "r", stdin); scanf ("% d", & N); memset (stack_num, 1, sizeof (stack_num); for (I = 0; I <N; I ++) stack [I] [0] = I; while (scanf ("% s", S1) & strcmp (S1, "quit ")! = 0) {scanf ("% d % S % d", & A, S2, & B); if (a = B) continue; for (I = 0; I <n; I ++) for (j = 0; j <stack_num [I]; j ++) if (stack [I] [J] =) {// locate the ID of the heap where A is located and the location of a in the heap astack = I; APOs = J;} else if (stack [I] [J] = B) {bstack = I; BPOs = J;} If (astack = bstack) continue;/* perform operations in four ways */If (strcmp (S1, "move ") = 0 & strcmp (S2, "onto") = 0) {back_initial (bstack, BPOs); back_initial (astack, APOs ); stack [bstack] [BPOs + 1] = A; stack_num [bstack] ++; stack_num [astack] --;} else if (strcmp (S1, "move ") = 0 & strcmp (S2, "over") = 0) {back_initial (astack, APOs); stack [bstack] [stack_num [bstack] =; stack_num [bstack] ++; stack_num [astack] --;} else if (strcmp (S1, "pile") = 0 & strcmp (S2, "") = 0) {back_initial (bstack, BPOs); for (I = apos; I <stack_num [astack]; I ++) stack [bstack] [stack_num [bstack] ++] = stack [astack] [I]; stack_num [astack] = apos;} else {for (I = apos; I <stack_num [astack]; I ++) stack [bstack] [stack_num [bstack] ++] = stack [astack] [I]; stack_num [astack] = APOs ;}for (I = 0; I <n; I ++) {printf ("% d:", I ); for (j = 0; j <stack_num [I]; j ++) printf ("% d", stack [I] [J]); putchar ('\ n');} return 0;} void back_initial (int s, int POS) {int I; for (I = stack_num [s]-1; i> Pos; I --) {stack [stack [s] [I] [stack_num [stack [s] [I] = stack [s] [I]; stack_num [s] --; stack_num [stack [s] [I] ++;} return ;}
The blocks problem UV 101