UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)
题目链接: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):
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
转载于:https://www.cnblogs.com/oyking/p/4503098.html
UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)相关推荐
- UVALive - 7139 Rotation 矩阵前缀和(imos和)
传送门:UVALive 7139 题意:N*M的网格,一辆车沿着网格线按给定路线走,每个网格里有一个人,人的视线始终看着车,问这些人净转圈数的平方和. 思路:因为起点和终点都是左上角,因此我们可以维护 ...
- 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 ...
- UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- The 2019 ICPC Asia Shanghai Regional Contest
The 2019 ICPC Asia Shanghai Regional Contest 题号 题目 知识点 A Mr. Panda and Dominoes B Prefix Code C Maze ...
- The 2014 ACM-ICPC Asia Mudanjiang Regional Contest(2014牡丹江区域赛)
The 2014 ACM-ICPC Asia Mudanjiang Regional Contest 题目链接 没去现场.做的网络同步赛.感觉还能够,搞了6题 A:这是签到题,对于A堆除掉.假设没剩余 ...
- 2014-2015 ACM-ICPC, Asia Tokyo Regional Contest
文章目录 2014-2015 ACM-ICPC, Asia Tokyo Regional Contest A.Bit String Reordering B.Miscalculation C.Shop ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest
The 2018 ACM-ICPC Asia Qingdao Regional Contest 青岛总体来说只会3题 C #include<bits/stdc++.h> using nam ...
- 2018 ICPC Asia Jakarta Regional Contest
2018 ICPC Asia Jakarta Regional Contest 题号 题目 知识点 难度 A Edit Distance B Rotating Gear C Smart Thief D ...
- 2018-2019 ACM-ICPC, Asia Jiaozuo Regional Contest题解
以下所有AC题解程序来自"仙客传奇"团队. A. Xu Xiake in Henan Province AC的C++语言程序: #include<iostream> # ...
最新文章
- java.lang.NoSuchMethodException: getPackageSizeInfo
- 二叉树和栈的基本操作
- bzoj4806 炮
- docker hub mysql主从_使用 Docker Compose 搭建 MySQL 数据库主从复制实例
- iOS分析友盟错误报告
- 2016考试计算机知识基础题库,2016年计算机二级公共基础知识基础练习题演练(6)...
- 20155313 杨瀚 《网络对抗技术》实验八 Web基础
- 用C语言来统计文件中单词的个数(C语言笔记)
- 机器视觉算法(数据结构)
- lisp型材库_基于AutoLISP的AutoCAD标准零件库的开发研究
- iOS播放器开发之MPMoviePlayerController
- PRML读书会第五章 Neural Networks(神经网络、BP误差后向传播链式求导法则、正则化、卷积网络)...
- 如何与ChatGPT交流,获得你想要的答案?正确提问是关键
- WPF——ViewBox控件
- 拉丁方阵(内置问题,渴望求解!)
- HDU 3105 Fred's Lotto Tickets(数学题)
- Pepper/Nao初级教程:第二章 Pepper的使用方法
- tftp服务器的配置
- Symantec 和sep卸载密码方法
- 惠普之路——HP公司发展史