Grid Game
InterviewTime limit1sMemory limit1024 MB
For each start cell on a grid with blocked cells, decide the winner of a game that moves a piece to the bottom-right corner with down, right, or diagonal moves of up to K cells.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Game theory
- Solved
- No attempts yet
Problem
Hanguk and Jeongol play a game on a grid board. Hanguk moves first, and the two players take turns moving a piece. Neither player may skip a turn.
The board has rows and columns, and some cells are blocked. The piece cannot move into a blocked cell. For convenience, the cell where the -th row (counting from the top) meets the -th column (counting from the left) is written as .
In one move, the piece moves down by one cell, right by one cell, or diagonally down and right by , , , cells. (The piece cannot move outside the board or into a blocked cell. If , the piece cannot move diagonally.)
Here are a few examples of the movement rules.
For example, take , , , and a board with no blocked cells. A piece at can move to 5 cells, marked with O in the figure below.

Now suppose and are blocked. Then the piece at can move to 3 cells, marked with O in the figure below.

In the next figure, , , , and no cells are blocked. A piece at can move to 3 cells, marked with O.

Finally, take , , , and no blocked cells. A piece at can move to 2 cells, marked with O.

The goal is to move the piece to the bottom-right cell . The player who makes the last move wins. Both players play optimally.
The winner can depend on the starting position. You are given positions on the board. For each position, find the winner when the game starts there.
Input
The first line contains three integers , , and , separated by spaces.
The next lines each contain a string of length made only of # and .. If the -th character of the -th line is #, cell is blocked. If it is ., the cell is not blocked.
The next line contains an integer .
Each of the next lines contains two integers and , separated by a space.
Output
For each of the positions, print First if Hanguk wins, or Second if Jeongol wins. Print one result per line, in order.
Constraints
- Cell is not blocked.
- From any unblocked cell, the piece can reach by following the movement rules.
- For every ():
- ,
- Cell is not blocked.
- is not .