#P2445. [SDOI2005] 动物园

[SDOI2005] 动物园

Description

The zoo located in the suburbs adopted then-advanced automated management facilities to manage the animals. However, because the system did not consider the 20002000 year problem, the staff became very worried. Although many preventive measures were taken, the system still developed some bugs around the turn of the century, and some cage doors opened automatically, letting the animals inside escape.

Fortunately, the zoo had already been closed, so animals will not leave the zoo. Sheriff Still received the alarm and led a team of officers to the scene. By then the animals had already gotten out of their cages, so the officers spent a lot of time bringing the situation under control, and all animals were sent to the plaza of the zoo. However, there was a tricky problem: since the system had completely crashed, it was impossible to know from which cage each animal had escaped. The officers remembered some of the animals’ movements, all in the following form:

At minute tt, a certain animal was seen at a certain position.

Still hopes to use these scattered pieces of information to determine from which cage each animal escaped.

Task

Based on the given information, write a program to determine the location of each animal’s cage.

The zoo’s terrain is described by an n×nn\times n grid. A cell can be either a building or open ground. Cage locations can only be on open ground, and animals only move on open ground. Each animal runs at a different speed. For example, a tiger can run 55 cells per minute, while a cat can only run 22 cells per minute, etc. The following is an example (the shaded cells are buildings):

Each cage holds exactly one animal, and different cages hold different animals. Different cages may be in the same cell.

Input Format

The first line contains a natural number nn, the side length of the grid.

The next nn lines form an n×nn\times n character matrix describing the zoo’s terrain. Here . denotes open ground, and * denotes a building.

The next line contains a natural number pp, the number of cages (which is also the number of animals).

The next pp lines each contain two natural numbers R,CR,C, indicating that a cage is at row RR, column CC of the grid.

The next pp lines each contain an integer ViV_i, in order giving the speed (cells/minute) of animal ii. Each animal is identified by its index.

The next line contains a natural number rr, the number of pieces of information provided by the officers.

The next rr lines each contain four natural numbers t,x,y,jt,x, y, j, meaning that at minute tt, animal jj was seen at position (x,y)(x, y) on the grid.

Refer to the sample for the specific format.

Output Format

Output any feasible solution.

Print pp lines, each with three integers i,x,yi,x,y, meaning that animal ii was originally at cell (x,y)(x,y).

5
.....
.***.
.....
.***.
.....
2
1 3
5 2
1
2
2
5 3 1 2
4 5 5 1

1 5 2
2 1 3

Hint

1n,p1001\leq n,p\leq 100x,ynx,y\leq n

Note: For certain testdata there may be multiple solutions; output any one of them.

Translated by ChatGPT 5