JOI Spring Training Camp 2015/16
Original Statement / Submission Site
JOI-kun is attending an IOI social gathering. At the gathering, sandwiches are arranged on a rectangular grid with R rows and C columns.
Each sandwich has the shape of a right isosceles triangle whose two perpendicular sides have the same length as one side of a grid cell. Two sandwiches are placed in each cell so that their hypotenuses touch each other.
A sandwich cannot be taken if both of the following conditions hold:
Any sandwich that does not satisfy both conditions may be taken.
Initially, no sandwiches have been taken. To take a particular sandwich, it may first be necessary to take several other sandwiches. Depending on the arrangement, some sandwiches may never be possible to take.
JOI-kun wants to eat both sandwiches placed in the same cell, but he has not yet decided which cell to choose. For each cell, he wants to know the minimum number of sandwiches that must be taken, starting from the initial state, in order to take both sandwiches in that cell.
Given the sandwich arrangement, determine for every cell whether it is possible to take both sandwiches in that cell. If it is possible, compute the minimum number of sandwiches that must be taken, including the two target sandwiches themselves.
The input is given as follows:
N or Z and represents the orientation of the two sandwiches in that cell.The characters N and Z correspond to the two possible diagonal orientations shown in Figure 2 of the original statement.
N and Z.Output R lines. The i-th line must contain C integers separated by spaces.
The j-th integer on the i-th line should be the minimum number of sandwiches that must be taken in order to take both sandwiches in the cell at row i, column j. Output -1 if it is impossible.
2 3 NZN ZZN
10 8 2 8 6 4
The sandwich arrangement in Sample Input 1 corresponds to Figure 1 in the original statement.
For example, to take both sandwiches in the cell at row 2, column 2, the sandwiches can be taken in the following order:
A total of 6 sandwiches are taken, and this is the minimum possible number. Therefore, the output for that cell is 6.
2 2 NZ ZN
-1 -1 -1 -1
In this case, none of the sandwiches can be taken.
5 5 NZZZN NNNZN NNZNN NZNNN NZZZN
10 12 14 16 2 8 -1 -1 -1 4 6 -1 -1 -1 6 4 -1 -1 -1 8 2 16 14 12 10