Push a Box
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

The barn can be modeled as an N×M rectangular grid. Some of the grid cells have hay in them. Bessie occupies one cell in this grid, and a large wooden box occupies another cell. Bessie and the box are not able to fit in the same cell at the same time, and neither can fit into a cell containing hay.

Bessie can move in the 4 orthogonal directions (north, east, south, west) as long as she does not walk into hay. If she attempts to walk to the space with the box, then the box will be pushed one space in that direction, as long as there is an empty cell on the other side. If there is no empty cell, then Bessie will not be able to make that move.

A certain grid cell is designated as the goal. Bessie's goal is to get the box into that location.

Given the layout of the barn, including the starting positions of the box and the cow, and the target position of the box, determine if it possible to win the game.

Note: This problem allows 512MB of memory usage, up from the default limit of 256MB.

输入描述:

The first line has three numbers, N, M, and Q, where N is the number of rows in the grid and MM is the number of columns.


On the next N lines is a representation of the grid, where characters represent empty cells (.), hay (#), Bessie's starting position (A), and the box's initial location (B).

This is followed by Q lines, each with a pair of integers (R,C). For each pair, you should determine if it is possible to get the box to that cell at row R, column C, starting from the initial state of the barn. The top row is row 1, and the left column is column 1.

输出描述:

Q lines, each with either the string "YES" or "NO".
示例1

输入

复制
5 5 4
##.##
##.##
A.B..
##.##
##.##
3 2
3 5
1 3
5 3

输出

复制
NO
YES
NO
NO

说明

To push the box to the position (3, 5), the cow just needs to move 3 spaces to the right.
None of the other three positions are attainable.