#P4601. [HEOI2012] 野外探险

[HEOI2012] 野外探险

Description

Using a GPS system, they obtained a rough map of the rainforest. The map is an nn-by-mm character grid: '.' denotes empty ground, '*' denotes impassable terrain, '#' denotes an area that needs to be cleared, and 'H' marks Xiao H's position, which is also empty ground.

There are tt types of movements, denoted by a[i]a[i], b[i]b[i] (1it1 \leq i \leq t). If they are currently at row xx, column yy, then their next position can be row x+a[i]x + a[i], column y+b[i]y + b[i], or the opposite direction, row xa[i]x - a[i], column yb[i]y - b[i]. Each day, they may choose exactly one movement type and move a positive number of steps continuously in the forward or backward direction, then stop; they are not allowed to stay put.

During that day’s movement, they may not enter any impassable cells ('*'). If they reach a cell marked '#', they stop moving immediately. For the rest of the day, they clear that cell; afterwards, it permanently becomes empty ground ('.').

They send you qq queries over the network, asking for the number of cells they can reach on day d[i]d[i] (1iq1 \leq i \leq q). It is guaranteed that there is no situation where they cannot move at all. Can you help them?

Input Format

  • The first line contains three positive integers nn, mm, and tt.
  • Lines 22 to n+1n+1: each line contains mm characters (each is one of '.', '*', '#', 'H'; there is exactly one 'H').
  • Lines n+2n+2 to n+t+1n+t+1: each line contains two integers a[i]a[i] and b[i]b[i] (not both 00).
  • Line n+t+2n+t+2: a single integer qq.
  • Lines n+t+3n+t+3 to n+t+q+2n+t+q+2: each of the next qq lines contains one integer d[i]d[i], the queried day.

Output Format

Output qq lines. For each query, output the number of cells that can be reached on day d[i]d[i].

3 3 2
H#.
*..
…
1 0
0 1
2
1
2 
1
4

Hint

Constraints:

  • For 20% of the testdata, n,m4n, m \leq 4, q100q \leq 100.
  • For 70% of the testdata, n,m300n, m \leq 300.
  • For 100% of the testdata, n,m1000n, m \leq 1000, t5t \leq 5, q1000q \leq 1000, 0d[i]1090 \leq d[i] \leq 10^9.

HEOI 2012 Day 2 Task 3.

Translated by ChatGPT 5