Battleship
Time limit1sMemory limit128 MB
Given two ship maps and a list of unlabeled shots, decide which admiral won under the hit-and-shoot-again turn rules.
- Level
Medium7 of 10
- Topics
- Simulation, Brute force, Implementation, Game theory
- Solved
- No attempts yet
Problem
When playing battleship, two players take turns trying to sink the other player's navy. Each shot is fired at a single coordinate. If a shot hits one of the enemy's ships and the enemy still has other ship parts left, the player may shoot again. Otherwise, the other player takes over. Once a ship part has been hit at some coordinate, shooting at that same coordinate again counts as a miss.
A navy is completely sunk when every part of every one of its ships has been hit. The first player starts, and both players take the same number of turns. Once one navy has been completely sunk the game ends at the end of that round; but because both players take the same number of turns, the second player still takes their turn during that round (even if all of their own ships have already been sunk), and that turn can still change the final result.
The result is then determined as follows. If exactly one navy has been completely sunk, the player whose navy survived wins. The game is a draw if both navies are completely sunk, or if neither navy is completely sunk after all shots have been fired (that is, ships still remain on both sides).
Tom, a little spy, watches a game of battleship between two fleet admirals. Having tapped the communication wires, he can intercept the shot orders, but he could not tell which admiral ordered which shot.
After the game he breaks into the super-secret game management facility and secures the fleet deployment maps. To decide which admiral is more dangerous, he wants to know who won.
He hands you the deployment maps and the shot orders and asks you to determine which admiral won.
Input
The input starts with a line containing the number of test cases ().
Each test case starts with a line containing three integers , , and (; ): the width and height of the deployment maps and the number of shots.
The next lines contain the deployment map for player one. Each line has field descriptions, where _ means water and # means a ship. The following lines contain the deployment map for player two.
The next lines contain the shot orders; each order is two integers, the and coordinates of the shot. The coordinate is the column, from to , where is the leftmost column. The coordinate is the row, from to , where is the last (bottom) line and is the first (top) line of the corresponding map.
Note that there may be more shot orders than are needed to finish the game.
Output
For each test case, print one of "player one wins", "player two wins", or "draw" on its own line.