Oily Currents
InterviewTime limit1sMemory limit128 MB
Simulate oil spreading on a grid for t steps, where each contaminated cell pushes oil up to its strength in its current's direction. Print the final contaminated cells.
- Level
Medium4 of 10
- Topics
- Simulation, BFS, Matrix, Implementation
- Solved
- No attempts yet
Problem
One major problem with something like an oil spill is that the oil does not simply stay where it comes from. If we only had a big cloud of oil forming right above the ruptured oil well itself, that would still be a big problem, but it would be much easier to clean up. Instead, the oil spreads both under the water and on top of it, driven by wind, waves, and currents. Once it covers hundreds of square miles, it becomes much harder to clean. Here we use a simple model to compute the area that ends up covered by oil because of currents. For simplicity we consider only two dimensions and a bounded region; any oil that leaves that region is assumed never to return to it.
The current information is given as follows. For a 2-dimensional grid, each cell tells you the strength (from to ) and the direction (N, S, E, or W) of the current. For instance, 2N means that once this cell becomes contaminated with oil, within one time step the two cells immediately to the north of it also become contaminated. In general, a contaminated cell with strength and direction contaminates, in the next time step, every cell from up to cells away in direction . Given the coordinates of the cell where the spill starts and the duration to simulate, output the map of the oil spread after those steps.
Here N means north (the row number decreases), S means south (the row number increases), E means east (the column number increases), and W means west (the column number decreases). Oil that spreads out of the grid disappears and never returns.
Input
The first line contains the number of data sets. This is followed by data sets, each of the following form.
The first line of a data set contains five integers . Here are the width and height of the map, and are the coordinates of the cell where the oil spill starts, and is the duration to simulate.
This is followed by lines, each consisting of characters. The characters in columns and of line describe the current at position . The first one (a digit between and ) is the strength, and the second one (N, S, E, or W) is the direction of the current.
Output
For each data set, output Data Set x: on a line by itself, where is its number (starting from ). Then output the map after steps of the oil spill. The map consists of lines of characters each. The -th character of line is a dot (.) if that cell was not reached by the oil, and X if it was reached. Output a blank line between consecutive data sets.