Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can someone help me code wise. I dont understand this, can someone code it so then I can review the code and understand for myself?

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

can someone help me code wise. I dont understand this, can someone code it so then I can review the code and understand for myself? THANK YOU!

Problem Given a growing list of ships on a toroidal grid over time, and locations of storms, determine which ships will need to be wamed. (STANDARD) Input Input will begin with a line containing 2 integers, W and H,(20W,H500,000), representing the beight and width of the grid respectively. Initially you are tracking no boats. Following this line will be a list of commands. The commands must be processed in the order they are given. Each command begins with a number and can be 1 of 4 types. The types are listed below, - 1y n This command informs us that a new ship is added to the fleet. The ship starts at location (x,y)(1xW;1yH). The value d will be a single letter, either ' L, ' R, ' D ', or ' D ' (quotes for clarity) representing the direction the new ship travels in (Left, Right, Down, or Up respectively). The name of the ship is denoted by the string n. The string n will contain at least 1 and at most 20 characters. All characters in n will be either upper or lower case letters. No 2 ships will have the same name. - 2t This command informs us that time is passing by the amount of minutes. All the ships will move t(1t525,600) units in their respective direction. Note that trying to update all the ships at this time will be prohibitively expensive timewise in some cases. The sum of the times across the whole program will be less than or equal to 1 billion (10). - 3y h This command informs us that a storm will appear. The "bottom left" cell affected by the storm will be at location (x,y)(1xW;1yH), and the storm will be w units wide (across the x ) and h units high (across the y ) (1w10;1h10 ). Note a ship location (x+w,y) will NOT be affected by the storm. Additionally, storms can also wrap around 2D grid. - 4 This command requests that the program ends (I hope the company is still in business). (STANDARD) Output For each 3 command (storm notification) you will need to first print on a line by itself a non-negative integer (n), representing the number of boats currently in the storm. The following n lines should each contain a name of one of the ships affected by the storm. Explanation Case 1 The world in this case looks like the followine (Note shading and bolded lines are for clarity). We add a ship at position 414 and 2025 , which makes the world look like the following The next 2 commands place 2 storms. The storms locations both start at (2,12). The first storm is 2 wide and 10 tall. The second storm is 10 wide and 2 tall. Visually speaking the storms look like the following. Neither storm overlaps any ships, so the both result in an output of 0 . The next command adds a boat at position ( 2,12) (hopefully they weathered the storms) We then move time forward by a single minute, and end up with the following boat positions Storm 3 contains both BoatOne and BoatThree, so we output the number 2 (representing the number of ships in danger) followed by the names each on their own line. Storm 4 contains only Boat Three, so we output the number 1 (representing the number of ships in danger) followed by the name of the ship in danger on its own line. The arder of the names does not matter as long as they occur before the output for the next storm. Case 2 The side of the world is a 20 by 20 squareBelow is a grid representing the world for the second case (seain shadino and hold lines are for claritvl A storm oecurs starting at 2020 and has a width of 2 by 2 . The storm wraps around the world in both directions and the resulting image shows the location of the storm The ships in danger are BoatThree and BoatOne. The output is therefore the number 2 (for the number of boats in danger) and following this are the names of the boats in danger (again printed in any order). After this storm the boats move 1 unit. The resulting location of the boats can be visually seen in the following image There is another storm at the same location as the first. This time the boats affected are different. Below is an image of the storm location again. There are 3 boats in danger: BoatTwo, BoatFour, and BoatFive. The number 3 is printed on its own line. Following the number 3 is the names of the boats each on their own line. The next input line ends the program. Hints Reading Commands: To handle each command you need to read a different number of tokens depending on the command type. I strongly encourage reading the command type in a single scanf. After the command type is read in you can use an if statement to determine which command type was given, and within that if statement you can read the remaining tokens for the command. Storage: DO NOT STORE THE FULL GRID. You should use dynamic memory to expand what is needed. I recommend using an array list of ships. Moreover, I recommend using an Array List for each row and an Array List for each column. IMPORTANT: Ships that move leftright will always stay in their initial row. For this reason the left/right ships can be stored in the Array List for the rows. IMPORTANT: Similarly, ships that move up/down will always stay in their initial column. For this reason the up/down ships can be stored in the Array List for the columns. Time: DO NOT LOOP THROUGH ANY OF THE SHIPS TO MOVE THEM_ It takes a long time to do this for each command type 2 . Instead you can track the ship movement by storing what the current time is and storing at which time did a ship poof into existence. Storm Handling: DO NOT LOOP THROUGH ALL THE SHIPS TO FIND THE ONES IN DANGER. Instead loop only through the rows/columns which could contain the ships that are in peril. 0 VS 1 Indexing: Be careful with the x and y coordinates that your program reads. The ship and storm locations are given starting at 1. "Collisions": It can be the case that multiple boats occupy the same area at the same time. You can assume that the boats can give each other enough room to navigate within the same region. Base Code: Below is a list of structs, function prototypes, and comments I used in my solution. typedef struet Boat Boat; typedef struet ArrayList ArrayList; struet Boat \{ int start_ x, start_y, delta_ x, delta_y, start_time; char name [NaME_LEN +1]; h; struet ArrayList struct Cage * cages; int numCages; hi // appendTolist () comments // Check if the list is full // Expand the list // Add the boat to the end of the list // Update the 1 ist's size // query() comments // Create and initialize the answer // Loop through all the boats in the list // Get the eurrent boat // Get the eurrent location of the boat (based on time) // Find the offset of the boat from the storm // Wrap the offsets around the world if necessary (modulo) // Check if the boat is in the storm by using the offset

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions

Question

=+c) Is this process out of control?

Answered: 1 week ago

Question

Describe a department managers role in the union organizing process

Answered: 1 week ago