The blocks problem UV 101

Source: Internet
Author: User

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

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.