Guide mechs across open and ruined blocks to reach a clear row or column shot at Godzilla on his fixed path with the fewest homes destroyed.
Medium7BFSSimulationShortest pathNo attempts yetTime limit2sMemory limit256 MBGodzilla is rampaging through Tokyo again. The Superior Defender Mech Force (SDMF) has sent its mech units to stop the attack. A mech unit is a gigantic bipedal robot, usually piloted by a Japanese teenager, that carries a weapon of superior destruction. One hit from that weapon neutralizes Godzilla.
The SDMF faces two problems. First, a mech unit is so big that it cannot walk over a residential sector, because it would trample the people of Tokyo. Second, the weapon is so powerful that a pilot cannot fire it at Godzilla while any residential sector lies between the firing mech and Godzilla.
The SDMF wants to run some simulations before it faces the Godzilla threat. A simulation uses a two-dimensional map of the area of Tokyo where Godzilla is running amok. Time passes in rounds, and each round has two turns. On the first turn Godzilla makes a move. On the second turn the mech units make a move. A single move goes one sector North, East, South or West.
Godzilla is lacking in brain matter and his movement is completely predictable. He never moves into a sector he has already visited. On the first turn of each round he acts like this.
On the second turn of each round, each mech unit either stays where it is or moves into an adjacent non-residential sector or a ruined sector. It cannot move outside the map. When it moves, it goes one sector North, East, South or West. It may move into the sector of another mech unit. A mech can fire its weapon at Godzilla when there is a straight horizontal or vertical line between the mech and Godzilla and no residential sector obstructs that line. A mech that can fire does so on the same turn, and it may move and fire on that turn.
A residential sector destroyed on the first turn of a round still counts even when a mech fires later in that same round.
Given a map of the Tokyo area where Godzilla is rampaging and the starting positions of the mechs, determine the minimum number of residential sectors that will be destroyed before a mech unit neutralizes Godzilla.
The first line contains an integer T, the number of test cases. Each test case is given as follows.
The sector in the top left corner of the map is the northwest corner of Tokyo, and the sector in the bottom right corner is the southeast corner.
A simulation contains at most 100 mech units.
It is always possible for Godzilla to be neutralized by a mech unit.
For each test case, print one line containing a single integer, the minimum number of residential sectors that will be destroyed before a mech unit neutralizes Godzilla.