题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=648&page=show_problem&problem=5151
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.

题目大意:给一个n*m的矩阵,每个方块上有一个人。现在有一辆车在左上角的格点处,矩阵里的人都会一直面向那辆车。现在给出车的移动路线,问每个人总旋转角度的平方和是多少。若一个人顺时针旋转10个圈,逆时针旋转15个圈,最终算旋转角度为5个圈。
思路:根据题意,车一定会回到原点,那么每个人的初始面向方向与最终面向方向相同,每个人旋转的圈数都必将是整数。
若车在人的正左方下降了X次,上升了Y次,那么那个人的旋转圈数便是abs(X-Y)。(不要问我为什么,多想想多想想)

然后暴力模拟车的移动:

在车向下移动的时候,我们就要给车影响右方的一个矩阵加上1。
在车向上移动的时候,我们就要给车影响右方的一个矩阵减去1。
为了给一个矩阵加上一个值,因为本题只需要最终结果,可以使用静态的矩阵前缀和的方式处理。
最后求出矩阵,直接累加结果即可。

代码(0.249S):

 #ifdef OYK_JUDGE
#define longformat "%I64d"
#else
#define longformat "%lld"
#endif // OYK_JUDGE #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL; vector<vector<int> > mat;
int T, n, m, k; char str[] = "RDLU";
int fr[] = {, , , -};
int fc[] = {, , -, }; void modify(int c, int r1, int r2, int val) {
mat[r1][c] += val;
mat[r2][c] -= val;
} LL solve() {
int step, r = , c = ;
char op;
while(k--) {
scanf(" %c%d", &op, &step);
if(op == 'D') modify(c, r, r + step, );
if(op == 'U') modify(c, r - step, r, -); int f = strchr(str, op) - str;
r += step * fr[f];
c += step * fc[f];
} LL res = ;
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) {
mat[i][j] = mat[i][j] + mat[i - ][j] + mat[i][j - ] - mat[i - ][j - ];
res += mat[i][j] * mat[i][j];
}
return res;
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d%d", &n, &m, &k);
mat = vector<vector<int> >(n + , vector<int>(m + , ));
LL res = solve();
printf("Case #%d: " longformat "\n", t, res);
}
}

UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)的更多相关文章

  1. UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  2. UVALive 7141 BombX(离散化+线段树)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  3. UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  4. UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  5. UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)

    Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...

  6. UVALive 7148 LRIP(树的分治+STL)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  7. hdu5071 2014 Asia AnShan Regional Contest B Chat

    模拟题: add的时候出现过的则不再添加 close的时候会影响到top rotate(Prior.Choose)的时候会影响到top /*============================== ...

  8. 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy

    Galaxy Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...

  9. dp --- 2014 Asia AnShan Regional Contest --- HDU 5074 Hatsune Miku

    Hatsune Miku Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5074 Mean: 有m种音符(note),现在要从 ...

随机推荐

  1. MyEclipse10的一些问题(git插件,jdk7)

    egit: MyEclipse10 要装 egit2.3,版本错了安装不成功; jdk7: 10.5好像是不支持JDK1.7的,换成10.7; JDK1.7中的switch支持String类型的,1. ...

  2. 探索摸寻之XCode 快捷键

    注释/反注释 Command+/ 模拟器没有Home键 在模拟器的应用界面 Command+Shift+h 返回上一级 (1920,24寸显示器,因此不是分辨率问题)

  3. 【Unity3d游戏开发】UGUI插件入门之游戏菜单

    ugui是unity4.6开始加入的一个新的ui系统,非常强大,下面我们将通过一系列博客的方式一起来学习一下ugui的使用.本篇博客会介绍如何使用ugui制作一个游戏菜单,并且了解如何让物体与ugui ...

  4. 谓词 (NSPredicate)使用详情

    谓词 更加详细:http://blog.csdn.net/ztp800201/article/details/8116081 //判断是否满足条件 第一种 判断一个数组(array)中满足条件的 NS ...

  5. C语言_第三章

    1.常量        1.整型常量        2.实型常量                1.十进制小数形式,由数字和小数点组成.                2.指数形式(以E或e代表以10 ...

  6. 【Oracle】Oracle 序列步长问题

    问题: 数据库中客户表的ID 变化为 21\31\41 有序数字,而不是1\2\3 依次增长 [问题原因]: SEQ_CUSTOMNOTEEN 设置了缓存20,每次取20个数,然后一个一个给你,如果中 ...

  7. jquery mobile系列问题汇总整理(传播知识,利己利人)

    我在用jquery mobile做项目时,遇到jm在下拉框等组件里不能正常动态更新内容,查找了相关资料,在这里抛砖引玉,先提供一个解决下拉框内容写入更新的解决方法: jm解决下拉框内容写入的方法可以这 ...

  8. PAT自测-5 Shuffling Machine

    原题连接https://pta.patest.cn/pta/test/17/exam/4/question/264 Shuffling is a procedure used to randomize ...

  9. Mac 系统环境变量配置

    Mac 系统环境变量配置 例如这里要配置一下 QUICK_V3_ROOT 的环境变量 1.打开终端 输入  vim ~/.bash_profile 2.一直回车 知道出现以下选项 按 E 编辑     ...

  10. iOS 模仿一个小项目,总结一下里边的模块

      ManoBoo:  参考链接:http://www.jianshu.com/p/fd4c46c31508  这个小的项目是参考ManoBoo的简书的,链接在上方,自己在仿做的过程中,也离不开Man ...