Data Structure: Ultraviolet 101-the blocks Problem

Source: Internet
Author: User

 

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 37

 

Question meaning:

Start to specify the numbers 0-N respectively in the 1-N stack, and then perform four operations to find the data in the last n stacks.

Four operation rules:

1. Move a onto B

Return the data above stack a to the initial state, return the data above stack B to the initial state, and then place a on Stack B.

2. Move a over B

Return the data on stack a to the initial state, and then place stack a on Stack B.

3. Pile A onto B

Return the data above B's stack B to the initial state, and then move all the elements in the stack where A is located, including element A, to the stack where B is located.

4. Pile A over B

Move all the elements in the stack where A is located, including element A, to the stack where B is located.

 

Solution:

Use the stack to simulate the execution process. Note that the data is 0-n-1, and add one for regression. For details, see the code.

 

Code:

# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # define EPS 1e-6 # define Inf (1 <20) # define PI ACOs (-1.0) using namespace STD; struct stack // simulate stack {int top; int save [100]; int POP () {return save [top --];} int push (int n) {++ top; return save [Top] = N;} bool empty () {If (Top = 0) return true; return false;} Void clear () {Top = 0;} int finda (int A) {for (INT I = 1; I <= top; I ++) // locate the number in the stack if (save [I] = A) return I; return 0 ;}; stack [100]; int N; int find (int A) // find which stack a belongs to {for (INT I = 1; I <= N; I ++) {Int J = stack [I]. finda (a); If (j) return I ;}} void removeup (INT local, int) // return all the elements above a to {// while (stack [local]. save [stack [local]. top]! = A) int Limit = stack [local]. finda (a); For (INT I = Limit + 1; I <= stack [local]. top; I ++) {int temp = stack [local]. save [I]; stack [temp + 1]. push (temp);/* stack [temp + 1]. clear (); // note that the regression should correspond to the plus one, and the stack [temp + 1] is killed. push (temp); */} stack [local]. top = limit; // note the number of elements on the stack now return;} void removewhole (INT locala, int A, int localb) // move all the elements in stack A and above to stack B in sequence {int I = stack [locala]. finda (a); // first find the position of a int temp = I; for (; I <= ST Ack [locala]. top; I ++) {stack [localb]. push (stack [locala]. save [I]);} stack [locala]. top = temp-1; // update the number of elements in stack a return;} void moveonto (int A, int B) {int Tempa = find (), tempb = find (B); If (Tempa = tempb) return; removeup (Tempa, a); removeup (tempb, B); stack [tempb]. push (a); // move A to B stack [Tempa]. top --; return;} void moveover (int A, int B) {int Tempa = find (A), tempb = find (B); If (Tempa = tempb) return; removeup (TEM Pa, a); stack [tempb]. push (a); // move A to B stack [Tempa]. top --; return;} void pileonto (int A, int B) {int Tempa = find (A), tempb = find (B); If (Tempa = tempb) return; removeup (tempb, B); removewhole (Tempa, A, tempb); return;} void pileover (int A, int B) {int Tempa = find (), tempb = find (B); If (Tempa = tempb) return; removewhole (Tempa, A, tempb);} void print () {for (INT I = 1; I <= N; I ++) {printf ("% d:", I-1); If (! Stack [I]. empty () {for (Int J = 1; j <= stack [I]. top; j ++) printf ("% d", stack [I]. save [J]);} putchar ('\ n');} return;} int main () {int A, B; char COM1 [20], com2 [20]; while (CIN> N) {for (INT I = 1; I <= N; I ++) {stack [I]. clear (); stack [I]. push (I-1);} // print (); While (CIN> COM1 & strcmp (COM1, "quit ")) {scanf ("% d % S % d", & A, com2, & B); if (a = B) continue; If (strcmp (COM1, "move ") = 0) {If (strcmp (com2, "onto") = 0) moveonto (a, B); else moveover (a, B );} else {If (strcmp (com2, "onto") = 0) pileonto (a, B); else pileover (a, B);} // print ();} print ();} return 0 ;}

 

 

 

 

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.