Uva oj 101-the blocks problem (block problem)

Source: Internet
Author: User

Time Limit: 3.000 second
Time Limit: 3.000 seconds

 

Background
Background

Using areas of computer science use simple, abstract domains for both analytical and empirical studies. for example, an early AI Study of planning and Robotics (strips) used a block world in which a robot arm timed med tasks involving the manipulation of blocks.
Simple and abstract methods are used in many aspects of computer science for analysis and experimental research. For example, in the early days of planning and robotics, artificial intelligence used a world of blocks to allow robotic arm to execute the tasks of operating blocks.

In this problem you will model a simple block world under certain rules and constraints. rather than determine how to achieve a specified state, you will "program" a robotic arm to respond to a limited set of commands.
In this case, you will build a simple world of building blocks under certain rules and constraints. This is not to let you study how to reach a certain State, but to write a "mechanical armProgramTo respond to a limited set of commands.

 

The Problem
Problem

The problem is to parse a series of commands that instruct a robot arm in how to manipulate blocks that lie on a flat table. initially there are n blocks on the table (numbered from 0 to n-1) with block Bi adjacent to block Bi + 1 for all 0 ≤ I <n-1 as shown in the dimo-below:
The problem is to analyze a series of commands and tell the robotic arm how to manipulate the building blocks placed on a platform. At first, the platform had n building blocks (numbers from 0 to n-1). For any 0 ≤ I <n-1, the building block Bi is adjacent to Bi + 1, the figure is as follows:

 

Figure: initial blocks world
Figure: initial state of the world of building blocks

 

The valid commands for the robot arm that manipulates blocks are:
The valid commands for operating blocks of a robotic arm are as follows:

  • Move a onto B

    • Where A and B are block numbers, puts Block A onto Block B after returning any blocks that are stacked on top of blocks A and B to their initial positions.
    • Both A and B are the building blocks. Put all the building blocks on a and B back to their original places, and then put a on B.
  • Move a over B
    • Where A and B are block numbers, puts Block A onto the top of the stack containing Block B, after returning any blocks that are stacked on top of Block A to their initial positions.
    • Both A and B are the building blocks. Put all the building blocks on a back in the original place, and then put a on B. (The original building blocks on B do not move)
  • Pile A onto B
    • Where A and B are block numbers, moves the pile of blocks consisting of block A, and any blocks that are stacked abve block A, onto Block B. all blocks on top of Block B are moved to their initial positions prior to the pile taking place. the blocks stacked abve Block A retain their order when moved.
    • Both A and B are the building block numbers, and a and all the above positive components of a are moved to B. Before moving, you must put all the positive values above B back to the original position. The original sequence of moving blocks remains unchanged.
  • Pile A over B
    • Where A and B are block numbers, puts the pile of blocks consisting of block A, and any blocks that are stacked abve block A, onto the top of the stack containing Block B. the blocks stacked abve Block A retain their original order when moved.
    • Both A and B are the number of blocks, and move a and all the above positive components to the top block of B. The original sequence of moving blocks remains unchanged.
  • Quit
    • Terminates Manipulations in the block world.
    • End the operation of the world of building blocks.

Any command in which a = B or in which A and B are in the same stack of blocks is an illegal command. all illegal commands shoshould be ignored and shoshould have no affect on the configuration of blocks.
When a = B or A and B are in the same stack, any attempt to operate a and B is illegal. All invalid commands must be ignored and cannot affect the status of the current building block.

 

The input
Input

The input begins with an integer n on a line by itself representing the number of blocks in the block world. You may assume that 0 <n <25.
The input starts with an integer N. This integer occupies only one row, indicating the number of blocks in the world of blocks. You can assume 0 <n <25.

The number of blocks is followed by a sequence of block commands, one command per line. Your program shocould process all commands until the quit command is encountered.
Starting from the next line of the number of blocks, a series of commands exclusive to one line. Your program processes all the commands until you enter the exit command.

You may assume that all commands will be of the form specified above. There will be no syntactically incorrect commands.
You can assume that all commands are given in the format shown above. No syntax error occurs.

 

The output
Output

The output shoshould consist of the final state of the blocks world. each original block position numbered I (0 ≤ I <n where N is the number of blocks) shocould appear followed immediately by a colon. if there is at least a block on it, the colon must be followed by one space, followed by a list of blocks that appear stacked in that position with each block number separated from other block numbers by a space. don't put any trailing spaces on a line.
Outputs the final state of the world of building blocks. The position I (0 ≤ I <n, n is the number of blocks) of each original building block is followed by a colon. If at least one building block is located at this position, the colon is followed by a space followed by a sequence of all building block numbers in this position. The numbers of each two blocks are separated by a space. No extra space is allowed at the end of the line.

There shoshould be one line of output for each block position (I. e., n lines of output where N is the integer on the first line of input ).
Each building block occupies an exclusive row (that is, the N input in the first row, corresponding to the output n rows ).

 

Sample Input
Input example

10
Move 9 onto 1
Move 8 over 1
Move 7 over 1
Move 6 over 1
Pile 8 over 6
Pile 8 over 5
Move 2 over 1
Move 4 over 9
Quit

 

Sample output
Output example

0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:

 

Analysis
Analysis

Programming is cumbersome for simulated questions. There is no need to consider the situations where blocks are occupied in their original locations, because no operation can place blocks in an empty position, so they cannot appear in the I position, there are other building blocks under block I. There are two types of operations: one is to clear the blocks above (return), and the other is not required, so that the operations can be processed in a unified manner. The followingCodeThere is also an additional command: dump, you can view the current status at any time to facilitate debugging.

 

Solution
Answer

# Include <iostream> # include <string> using namespace STD; int astks [25] [25]; int APOs [25]; // indicates the position of each block, stack ID * int astklen [25]; int nstkcnt; struct command {int ncom; int NA; int Nb ;}; bool parsecommand (const char * pcomstr, command & cmd) {int NPOs = 0, I, j; static char * pcom [] = {"move", "pile", "quit", "dump "}; for (I = 0; I <sizeof (pcom)/sizeof (pcom [0]); ++ I) {for (j = 0; pcomstr [J] = pcom [I] [J] & & Pcom [I] [J]! = 0; ++ J); If (pcom [I] [J] = 0) break;} if (I> = sizeof (pcom) /sizeof (pcom [0]) {return false;} cmd. ncom = I * 2; If (CMD. ncom> = 4) {return true;} If (pcomstr [J] <'1' | pcomstr [J]> '9') {return false;} For (CMD. NA = 0; pcomstr [J]> = '0' & pcomstr [J] <= '9'; ++ J) {cmd. NA = cmd. na * 10 + pcomstr [J]-'0';} static char * pdir [2] = {"onto", "over"}; for (I = 0, pcomstr + = J; I <2; ++ I) {for (j = 0; PC Omstr [J] = pdir [I] [J] & pdir [I] [J]! = 0; ++ J); If (pdir [I] [J] = 0) {break;} if (I >=2) {return false;} cmd. ncom + = I; If (pcomstr [J] <'1' | pcomstr [J]> '9') {return false;} For (CMD. NB = 0; pcomstr [J]> = '0' & pcomstr [J] <= '9'; ++ J) {cmd. NB = cmd. NB * 10 + pcomstr [J]-'0';} If (pcomstr [J]! = 0 | cmd. Na = cmd. nb) {return false;} return true;} void returnupperblock (INT nbaseblk) {// calculate the stack and height of the building block. Nstk is the stack ID, and nhei is the stack height int nstk = APOs [nbaseblk]/nstkcnt, nhei = APOs [nbaseblk] % nstkcnt + 1; // cyclically normalize all blocks above the specified block for (Int & nstklen = astklen [nstk]; nstklen> nhei; -- nstklen) {int nupperblk = astks [nstk] [nstklen-1]; // The top block number astks [nupperblk] [0] = nupperblk; // convert it to the original position APOs [nupperblk] = nupperblk * nstkcnt; // re-calculate and synthesize the building block location astklen [nupperblk] = 1; // set the height of the destination stack to 1 (only the building block of the homing)} void dump (void) {for (INT I = 0; I <Nstkcnt; ++ I, cout <Endl) {cout <I <':'; For (Int J = 0; j <astklen [I]; ++ J) {cout <''<astks [I] [J] ;}} int main (void) {command cmd; CIN> nstkcnt; For (INT I = 0; I <nstkcnt; ++ I) {astks [I] [0] = I; APOs [I] = I * nstkcnt; astklen [I] = 1 ;} for (string strline; Getline (CIN, strline);) {If (! Parsecommand (strline. c_str (), CMD) {continue;} If (CMD. ncom = 4) {break; // quit} If (CMD. ncom = 6) {dump (); continue;} int nstka = APOs [cmd. na]/nstkcnt, nstkb = APOs [cmd. NB]/nstkcnt; If (nstka = nstkb) {continue;} If (CMD. ncom % 2 = 0) {returnupperblock (CMD. NB); // onto} If (CMD. ncom/2 = 0) {returnupperblock (CMD. na); // move} int nheia = APOs [cmd. na] % nstkcnt; int token = astklen [nstka], & nstklenb = astklen [nstkb]; for (astklen [nstka] = nheia; nheia <nstklena; ++ nheia) {int nblk = astks [nstka] [nheia]; APOs [nblk] = nstkb * nstkcnt + nstklenb; astks [nstkb] [nstklenb ++] = nblk ;}} dump (); 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.