Hoseok, the Patient Domino Artisan
InterviewTime limit1sMemory limit512 MB
Simulate R rounds of domino pushes and single-cell repairs on a grid, handling chain reactions, and report the attacker's total score and final board.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, BFS, Queue
- Solved
- No attempts yet
Problem
There are many ways to make a person angry. The nastiest one is knocking over dominoes they worked hard to stand up. The board game released this time, "You Die and I Live Game," uses exactly this idea: two players attack and defend. The attacker keeps knocking dominoes over, and the defender keeps standing them up. The game proceeds as follows.
- Dominoes are placed on every cell of an N-row by M-column two-dimensional grid board. Each domino has a height between 1 and 5.
- In each round, the attacker attacks first, and the defender defends after the attack ends.
- The attacker knocks over the domino on a chosen cell in one of the four directions: east, west, south, or north. When a domino of length K falls in a given direction, the dominoes that have not yet fallen among the K-1 dominoes in that direction fall in the same direction one after another. Naturally, due to the nature of dominoes, a chain reaction of falling can occur. If the attacker attacks a cell whose domino has already fallen, nothing happens.
- The defender can stand up exactly one chosen domino among the fallen ones. If the defender tries to stand up a domino that has not fallen, nothing happens.
- Steps 3 and 4 repeat for a total of R rounds. Each round, the attacker counts the dominoes knocked over in that round, and the total over the R rounds is the attacker's score.

This is an example picture of a domino attack. The red numbers in the picture indicate dominoes that have fallen.
For example, when dominoes of heights {3, 1, 2, 2, 2} stand in a row, pushing the 3rd one to the right knocks the three on the left to the right. This causes a chain reaction among the newly fallen dominoes, and in the process the 2nd-length domino at the 4th position also falls. Then the last domino falls as well.
Hoseok, who prides himself on being nothing without patience, has volunteered as the defender to withstand your attacks. Given the initial state of the domino board and the record of both players' actions in each round, write a program that outputs the attacker's score and the final state of the board.
Input
The first line gives the number of rows N, the number of columns M, and the number of rounds R of the board.
Then N lines give the state of the board. They start from row 1, and the M numbers mean the length of the domino placed on each cell.
Then over R×2 lines, the actions of the attacker and defender are given. Each round consists of two lines: the first is the attacker's action, and the second is the defender's action. The attacker's action is given as "X Y D". This means pushing the domino at row X, column Y in direction D. D is one of E, W, S, N, meaning east, west, south, and north respectively. The defender's action is given as "X Y". This means standing the domino at row X, column Y back up.
If the attacker tries to knock over a domino on a cell that has already fallen, nothing happens. Also, if the defender tries to stand up a domino that has not fallen, nothing happens.
Output
On the first line, output the attacker's score.
Then output the state of the board over N lines. For each cell, output F if it has fallen and S if it has not, separated by spaces.
Constraints
All input values are positive integers.
- 1 ≤ N, M ≤ 100
- 1 ≤ R ≤ 10,000
- 1 ≤ domino length ≤ 5
- The attacker and defender never act outside the grid.