Two groups of cavemen got into a land dispute and decided to settle it the old-fashioned way, by throwing sticks at each other. The fight was organized in a cave, high enough that the ceiling is of no concern, but mineral deposits on the ground get in the way of flying sticks.
The cave can be divided into
One group of cavemen is on the left side of the cave, the other on the right side. The groups alternate throwing sticks at the other group; first a group chooses the height at which the stick will fly and then (climbing on each others' shoulders as necessary) they throw it and the stick flies horizontally through the cave at the chosen height.
If the stick hits a chunk of mineral on the way, it destroys the chunk, the cell becomes empty and the stick stops its journey.
When a chunk is destroyed, it is possible that a cluster falls apart. If a newly created cluster would float in the air, then it falls down because of gravity. While falling, the cluster does not change shape i.e. all chunks in it fall together. As soon as some chunk in the falling cluster lands on a chunk from a different cluster or the ground, the entire cluster stops falling. Of course, if a cluster lands on another, they merge and become one.
Your program will be given the layout of minerals in the cave and the heights at which sticks were thrown. Determine the layout of minerals after the sticks are exchanged.
Input Specification
The first line contains two integers .
represents an empty cell, while
the letter x
represents a chunk of mineral.
The next line contains an integer
The last line contains
No cluster will initially float in the air. Also, the input data will be such that at no point will two or more clusters fall simultaneously, so that there will be no ambiguous situations.
Output Specification
The output should consist of
Sample Input 1
5 6
......
..xx..
..x...
..xx..
.xxxx.
1
3
Sample Output 1
......
......
..xx..
..xx..
.xxxx.
Sample Input 2
8 8
........
........
...x.xx.
...xxx..
..xxx...
..x.xxx.
..x...x.
.xxx..x.
5
6 6 4 3 1
Sample Output 2
........
........
........
........
.....x..
..xxxx..
..xxx.x.
..xxxxx.
Explanation for Sample Output 2
The first stick destroys the chunk in the fourth column at height 6, the second destroying the chunk in the seventh column of the same row.
The third stick destroys the chunk in the third column at height 4, after which the starting cluster splits in two, but both new clusters still lay on the ground.
The fourth stick destroys the chunk in the seventh column at height 3, splitting the right cluster into two. The largest cluster would float in the air and falls two cells down.
Finally, the fifth stick destroys a chunk in the second column at height 1.
Sample Input 3
7 6
......
......
xx....
.xx...
..xx..
...xx.
....x.
2
6 4
Sample Output 3
......
......
......
......
..xx..
xx.xx.
.x..x.
Comments