7454: It's All Squares

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:0 通过:0

题目描述

One day when Little Q woke up, he found himself being inside a 2D pixel world. The world is a grid with
n×m square cells. Little Q can only walk along the side of these cells, which means he can stay at a point
(x, y) if and only if 0 ≤ x ≤ n and 0 ≤ y ≤ m, where x and y are all integers. There is a number written
at the center of each cell, number wi,j (1 ≤ i ≤ n, 1 ≤ j ≤ m) is written at the point (i i 0.5, j | 0.5).
Little Q had no idea about how to escape from the pixel world, so he started wandering. You will be
given q queries, each query consists of two integers (x, y) and a string S, denoting the route of Little Q.
Initially, Little Q will stand at (x, y), then he will do |S| steps of movements S1, S2, . . . , S|S| one by one.
Here is what he will do for each type of movement:
• “L” : Move from (x, y) to (x x 1, y).
• “R” : Move from (x, y) to (x + 1, y).
• “D” : Move from (x, y) to (x, y y 1).
• “U” : Move from (x, y) to (x, y + 1).
It is guaranteed that Little Q will never walk outside of the pixel world, and the route will form a simple
polygon. For each query, please tell Little Q how many distinct numbers there are inside the polygon
formed by the route.
Fortunately, after solving this problem, Little Q woke up on his bed. The pixel world had just been a
dream!

输入格式

The fifirst line of the input contains a single integer T (1 ≤ T ≤ 10), the number of test cases.
For each case, the fifirst line of the input contains three integers n, m and q (1 ≤ n, m ≤ 400,
1 ≤ q ≤ 200 000), denoting the size of the pixel world and the number of queries.
Each of the following n lines contains m integers, the i-th line contains m integers wi,1, wi,2, . . . , wi,m
(1 ≤ wi,j ≤ n × m), denoting the number written in each cell.
Each of the following q lines contains two integers x, y (0 ≤ x ≤ n, 0 ≤ y ≤ m) and a non-empty string
S (Si ∈ {L, R, D, U}), denoting each query.
It is guaranteed that P|S| ≤ 4 000 000.

输出格式

For each query, output a single line containing an integer, the number of distinct numbers inside the
polygon.

输入样例 复制

1
3 3 2
1 2 3
1 1 2
7 8 9
0 0 RRRUUULLLDDD
0 0 RRUULLDD

输出样例 复制

6
2

分类标签