Gravity
InterviewTime limit1sMemory limit512 MB
Apples fall straight down until they rest on obstacles or the floor; print the final grid.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
Simulate gravity on a two-dimensional grid of rows and columns.
Each cell holds an obstacle, holds a single apple, or is empty.
Apply the following rules until no further change is possible.
- Obstacles never move.
- If the cell immediately below an apple is empty, the apple moves into that cell.
Find the state of the grid after every apple has settled.
Input
The first line contains two integers and separated by a space. ()
Each of the next lines contains characters describing one row of the grid, from top to bottom. An obstacle is #, an apple is o, and an empty cell is ..
Output
Print the state of the grid on lines after all of the rules above have been applied.