# UVALive 7139 Rotation（矩阵前缀和）（2014 Asia Shanghai Regional Contest）

You have a rectangle of size N × M, N rows from top to bottom and M columns from left to right,
that is divided into a grid of unit squares. The corners and sides of those squares will be called grid
points and grid lines, respectively.
You are given a path along some grid lines. The path satisfies the following properties:
• Both start and end of the path are at the top left grid point.
• Each step is to go along the grid line (i.e., move up, down, left, or right).
You need to calculate the square sum of all the rotation values in each all. The definition of the
notation value in each cell is below.
Suppose there is a moving car at the path and a person stands at the center of the cell. The person
is facing the car all the time. After the path is finished, the rotation value of the grid equals to the
net number of clockwise turns the person would make if he stood in that square. (In other words, if
the person standing in that square rotate by the same total amount clockwise and counterclockwise,
the rotation value is 0. If the person’s total clockwise rotation is 360x degrees more than the person’s
total counterclockwise rotation, the rotation value of the cell is x. If the person’s total couuterclockwise
rotation is 360x degrees more than the person’s total clockwise rotation, the rotation value of the cell
is −x)
Input
The first line of the input gives the number of test cases, T. T cases follow. For each test case, the first
line contains three numbers, N, M and K. The next K line describes the steps of the path. Each line
containing ‘d s’, where d is one of the four characters (‘U’ for up, ‘D’ for down, ‘L’ for left, ‘R’ for right)
means the direction of the step and s is the length of the step.
It is guaranteed that the path is inside the grid.
Output
For each test case, output one line containing ‘Case #x: y’, where x is the test case number (starting
from 1) and y is square sum of all the rotation values of each cell.  1 #ifdef OYK_JUDGE
2 #define longformat "%I64d"
3 #else
4 #define longformat "%lld"
5 #endif // OYK_JUDGE
6
7 #include <iostream>
8 #include <cstdio>
9 #include <algorithm>
10 #include <cstring>
11 #include <vector>
12 using namespace std;
13 typedef long long LL;
14
15 vector<vector<int> > mat;
16 int T, n, m, k;
17
18 char str[] = "RDLU";
19 int fr[] = {0, 1, 0, -1};
20 int fc[] = {1, 0, -1, 0};
21
22 void modify(int c, int r1, int r2, int val) {
23     mat[r1][c] += val;
24     mat[r2][c] -= val;
25 }
26
27 LL solve() {
28     int step, r = 1, c = 1;
29     char op;
30     while(k--) {
31         scanf(" %c%d", &op, &step);
32         if(op == 'D') modify(c, r, r + step, 1);
33         if(op == 'U') modify(c, r - step, r, -1);
34
35         int f = strchr(str, op) - str;
36         r += step * fr[f];
37         c += step * fc[f];
38     }
39
40     LL res = 0;
41     for(int i = 1; i <= n; ++i)
42         for(int j = 1; j <= m; ++j) {
43             mat[i][j] = mat[i][j] + mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1];
44             res += mat[i][j] * mat[i][j];
45         }
46     return res;
47 }
48
49 int main() {
50     scanf("%d", &T);
51     for(int t = 1; t <= T; ++t) {
52         scanf("%d%d%d", &n, &m, &k);
53         mat = vector<vector<int> >(n + 2, vector<int>(m + 2, 0));
54         LL res = solve();
55         printf("Case #%d: " longformat "\n", t, res);
56     }
57 }
View Code

posted @ 2015-05-14 13:31  Oyking  阅读(656)  评论(0编辑  收藏