Under construction

Still being built. Things may change

Oily Currents

Interview

Time limit1sMemory limit128 MB

Summary
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 00 to 44) 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 ss and direction dd contaminates, in the next time step, every cell from 11 up to ss cells away in direction dd. Given the coordinates of the cell where the spill starts and the duration tt to simulate, output the map of the oil spread after those tt 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 KK of data sets. This is followed by KK data sets, each of the following form.

The first line of a data set contains five integers x,y,x0,y0,tx, y, x_0, y_0, t. Here 1≤x,y≤1001 \le x, y \le 100 are the width and height of the map, 1≤x0≤x1 \le x_0 \le x and 1≤y0≤y1 \le y_0 \le y are the coordinates of the cell where the oil spill starts, and 0≤t≤1000 \le t \le 100 is the duration to simulate.

This is followed by yy lines, each consisting of 2x2x characters. The characters in columns 2j−12j-1 and 2j2j of line ii describe the current at position (j,i)(j, i). The first one (a digit between 00 and 44) 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 xx is its number (starting from 11). Then output the map after tt steps of the oil spill. The map consists of yy lines of xx characters each. The jj-th character of line ii 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.

Examples3

  1. Example 1

    Input
    1
    15 7 8 4 5
    2W2S2W2S4W1S2W2W2S0E2S4E2S2E2S
    0E0E0S0N0S2E1E1N0E0W0S0N0E0S0N
    0E0E0S0N0S0E0E1E4S0W0S0N0E0S0N
    4W4W4W4W4W4W4W4N4E4E4E4E4E4E4E
    4W4W4W4W4W4W4W4W3E3E3E3E3E3E3E
    4W4W4W4W4W4W4W4W2E2E2E2E2E2E2E
    4W4W4W4W4W4W4W4W4W4E4E4E4E4E4E
    
    Expected output
    Data Set 1:
    XXXXXXXX.......
    .X.X.XXX.......
    .X.X...XX......
    .......XXXXXXXX
    ........XXXXXXX
    ........XXXXX..
    XXXXXXXXX......
    
  2. Example 2

    Input
    1
    3 3 2 2 0
    0N0N0N
    0N0N0N
    0N0N0N
    
    Expected output
    Data Set 1:
    ...
    .X.
    ...
    
  3. Example 3

    Input
    1
    5 1 1 1 1
    3E0E0E0E0E
    
    Expected output
    Data Set 1:
    XXXX.