This page is still under construction.

Parts of this page are still being built. What you see may change.

Mineral 2

Interview

Time limit1sMemory limit512 MB

Summary
Throw sticks alternately from left and right; each destroys the first mineral hit, then any cluster left floating falls straight down until it lands.
Level

Medium6 of 10

Topics
Simulation, Graph, BFS, Implementation
Solved
No attempts yet

Problem

Changyoung and Sanggeun are fighting over ownership of a cave. They decided to settle the matter by throwing sticks at each other. The fight takes place in the cave. The cave stores minerals, and a thrown stick can destroy them.

The cave is represented as R rows and C columns, or R×C cells. Each cell is either empty or contains a mineral. Two cells that contain minerals and are adjacent in one of the four directions belong to the same cluster.

Changyoung stands on the left side of the cave, and Sanggeun stands on the right. They throw sticks in turn. Before throwing a stick, they choose the height at which to throw it. The stick flies parallel to the ground.

When the stick meets a mineral in flight, the mineral in that cell is destroyed and the stick stops there.

After a mineral is destroyed, the remaining clusters can split apart. If a newly created cluster is floating in the air, it falls to the floor under gravity. The shape of a cluster does not change while it falls. A cluster keeps falling until it meets another cluster or the ground. It can land on top of another cluster, and after that the two merge.

Given the arrangement of minerals in the cave and the heights of the sticks thrown by the two players, write a program to find the arrangement of minerals after all sticks have been thrown.

Input

The first line gives the size of the cave, R and C. (1 ≤ R,C ≤ 100)

The next R lines each give C characters, where '.' is an empty cell and 'x' is a mineral.

The next line gives the number of sticks thrown, N. (1 ≤ N ≤ 100)

The last line gives the heights at which the sticks were thrown, separated by spaces. Every height is between 1 and R, where height 1 means the bottom row of the grid and R means the top. The first stick was thrown from left to right, the second from right to left, and so on, alternating direction.

No mineral cluster floats in the air, and no two or more clusters fall at the same time.

Output

Print the arrangement of minerals in the same format as the input.

Examples3

  1. Example 1

    Input
    5 6
    ......
    ..xx..
    ..x...
    ..xx..
    .xxxx.
    1
    3
    
    Expected output
    ......
    ......
    ..xx..
    ..xx..
    .xxxx.
    
  2. Example 2

    Input
    8 8
    ........
    ........
    ...x.xx.
    ...xxx..
    ..xxx...
    ..x.xxx.
    ..x...x.
    .xxx..x.
    5
    6 6 4 3 1
    
    Expected output
    ........
    ........
    ........
    ........
    .....x..
    ..xxxx..
    ..xxx.x.
    ..xxxxx.
    
  3. Example 3

    Input
    7 6
    ......
    ......
    xx....
    .xx...
    ..xx..
    ...xx.
    ....x.
    2
    6 4
    
    Expected output
    ......
    ......
    ......
    ......
    ..xx..
    xx.xx.
    .x..x.