Va OJ 114-simulation wizardry (simulate a pinball table)

Source: Internet
Author: User

Time Limit: 3.000 seconds

 

Background

Simulation is an important application area in Computer Science involving the development of computer models to provide insight into real-world events. there are running kinds of Simulation Including (and certainly not limited to) Discrete Event Simulation and clock-driven simulation. simulation often involves approximating observed behavior in order to develop a practical approach.

This problem involves the simulation of a simplistic pinball machine. in a pinball machine, a steel ball rolls around und a surface, hitting varous objects (bumpers) and accruing points until the ball "disappears" from the surface.

 

The Problem

You are to write a program that simulates an idealized pinball machine. this machine has a flat surface that has some obstacles (bumpers and Wils ). the surface is modeled as an m × n grid with the origin in the lower-left corner. each bumper occupies a grid point. the grid positions on the edge of the surface are Wils. bils are shot (appear) one at a time on the grid, with an initial position, direction, and lifetime. in this simulation, all positions are integral, and the ball's ction is one of: Up, down, left, or right. the ball bounces around the grid, hitting bumpers (which accumulates points) and Wals (which does not add any points ). the number of points accumulated by hitting a given bumper is the value of that bumper. the speed of all bils is one grid space per timestep. A ball "hits" an obstacle during a timestep when it wowould otherwise move on top of the bumper or wall grid point. A hit causes the ball to "rebound" by turning right (clockwise) 90 degrees, without ever moving on top of the obstacle and without changing position (only the direction changes as a result of a rebound ). note that by this definition sliding along a wall does not constitute "hitting" that wall.

A ball's life time indicates how many times units the ball will live before disappearing from the surface. the ball uses one unit of lifetime for each grid step it moves. it also uses some units of life time for each bumper or wall that it hits. the lifetime used by a hit is the cost of that bumper or wall. as long as the ball has a positive life time when it hits a bumper, it obtains the full score for that bumper. note that a ball with lifetime one will "die" during its next move and thus cannot obtain points for hitting a bumper during this last move. once the lifetime is non-positive (less than or equal to zero), the ball disappears and the game continues with the next ball.

 

The input

Your program shoshould simulate one game of pinball. there are several input lines that describe the game. the first line gives integers m and n, separated by a space. this describes a Cartesian grid where 1 ≤ x ≤ m and 1 ≤ y ≤ n on which the game is "played ". it will be the case that 2 <m <51 and 2 <n <51. the next line gives the integer cost for hitting a wall. the next line gives the number of bumpers, an integer p ≥ 0. the next P lines give the X position, y position, value, and cost, of each bumper, as four integers per line separated by space (s ). the X and Y positions of all bumpers will be in the range of the grid. the value and cost may be any integer (I. E ., they may be negative; a negative cost adds lifetime to a ball that hits the bumper ). the remaining lines of the file represent the bils. each line represents one ball, and contains four integers separated by space (s): The initial X and Y position of the ball, the direction of movement, and lifeits time. the position will be in range (and not on top of any bumper or wall ). the direction will be one of four values: 0 for increasing x (right), 1 for increasing y (up), 2 for decreasing X (left ), and 3 for decreasing y (down ). the lifetime will be some positive integer.

 

The output

There shoshould be one line of output for each ball giving an integer number of points accumulated by that ball in the same order as the bils appear in the input. after all of these lines, the total points for all bils shoshould be printed.

 

Sample Input

4
0
2
2 2 1 0
3 3 1 0
2 3 1 1
2 3 1 2
2 3 1 3
2 3 1 4
2 3 1 5

 

Sample output

0
0
1
2
2
5

 

Analysis
Analysis

The title is a simulated type. You must read the question carefully and never miss any details.

When a ball encounters an obstacle (a wall or a bullet Board), it only turns and does not move. However, whether or not the ball moves, the life value must be reduced by 1, because each step consumes one unit of time, the reduced life value when encountering an obstacle is calculated separately. Before moving the ball to the position of the bullet Board (the next step will be taken back to the original position), the life value must be positive. Otherwise, the score cannot be obtained because the life of the ball ends during the moving process. The ball cannot stay in the position of any obstacle. Once a collision occurs, the ball must return to its original position immediately. Assuming that the desktop size is 3 × 3, it is impossible to have a bullet board on the desktop, because there is only one cell in the circle for the ball to launch. In this case, the ball will not get any score, and the life value will be exhausted by hitting the wall. The coordinates given in the question start with 1.

 

Solution
Answer
# Include <iostream> using namespace STD; struct point {int X; int y ;}; // struct that records the bullet Board information, struct bumper {int nvalue; int ncost ;}; int main (void) {// use a two-dimensional array to record all the information of the bullet board. The position of the non-bullet board is null pointer bumper * ptable [52] [52] = {0 }, * pbumper; // set the moving offset point ptsize, ptpos, Adir [4] ={{ 1, 0 },{ 0, 1} according to the direction number specified by the question }, {-1, 0 },{ 0,-1 }}; int nwcost, ntotal = 0, nvalue, nlife, NDIR; // enter the desktop size, Wall consumption, and number of panels for (CIN> ptsize. x> ptsize. y> nwcost >>> Ntotal; ntotal --> 0;) {// read the coordinates, scores, and consumption of each bullet board cyclically, and save them to the array CIN> ptpos. x> ptpos. y; pbumper = new bumper; CIN> pbumper-> nvalue> pbumper-> ncost; // the coordinate of the bullet Board starts from 1, the array address ptable [ptpos. x-1] [ptpos. y-1] = pbumper;} // The desktop size also needs to be adapted to the array address ptsize starting with 0. x-= 1, ptsize. y-= 1; // read the starting coordinates of each ball cyclically, starting direction and life value for (ntotal = 0; CIN> ptpos. x> ptpos. y> NDIR> nlife;) {// adapt coordinates to the array address -- ptpos. x, -- ptpos. y; // move the ball cyclically until the lifecycle ends for (NV Alue = 0; -- nlife> 0;) {// create a new coordinate variable to store the value after moving, so that the point ptnew = {ptpos can be rolled back when moving fails. X + Adir [NDIR]. x, ptpos. Y + Adir [NDIR]. y}; // If (ptnew. X = ptsize. X | ptnew. y = ptsize. Y | ptnew. x <1 | ptnew. Y <1) {// The life value minus the Wall consumption and changes to nlife-= nwcost; NDIR = (NDIR + 3) % 4; continue ;} // If (pbumper = ptable [ptnew. x] [ptnew. y])! = 0) {// Add the score of the bullet board to the score. The life value minus the consumption and turns to nvalue + = pbumper-> nvalue; nlife-= pbumper-> ncost; NDIR = (NDIR + 3) % 4; continue;} // move to the blank grid ptpos = ptnew;} // accumulate the total score this time and output the current score ntotal + = nvalue; cout <nvalue <Endl;} // total output score,Program End cout <ntotal <Endl; 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.