UVALive 7139 Rotation(模拟)

思路:模拟题,留意到其实左右操作是会抵消的,所以只用考虑上下就好了


#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
using namespace std;
#define LL long long
vector<vector<int> >mp;
char *str = "UDLR";
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int main()
{
    int T,cas=1;
	scanf("%d",&T);
	while(T--)
	{
		int n,m,k;
		scanf("%d%d%d",&n,&m,&k);
		int r = 1,c=1;
	    mp = vector<vector<int> >(n+5,vector<int>(m+5));
		for (int i = 0;i<k;i++)
		{
             char op;
			 int s;
			 scanf(" %c%d",&op,&s);
			 if (op=='U')
			 {
				 mp[r-s][c]-=1;
				 mp[r][c]+=1;
			 }
			 if (op=='D')
			 {
				 mp[r][c]+=1;
				 mp[r+s][c]-=1;
			 }
			 int d = strchr(str,op)-str;
			 r+=s*dir[d][0];
			 c+=s*dir[d][1];
		}
		LL ans =0;
		for(int i = 1;i<=n;i++)
		{
			for (int j = 1;j<=m;j++)
			{
				mp[i][j]+=mp[i-1][j]+mp[i][j-1]-mp[i-1][j-1];
				ans+=mp[i][j]*mp[i][j];
			}
		}
		printf("Case #%d: %lld\n",cas++,ans);
	}
}



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.
Limits:
1 ≤ T ≤ 100
1 ≤ N, M
1 ≤ N × M ≤ 106
1 ≤ K ≤ 104
The path is valid.
Sample Input
4
1 1 4
R 1
D 1
L 1
U 1
1 1 4
D 1
R 1
U 1
L 1
2 2 6
R 1
D 2
R 1
U 1
L 2
U 1
2 2 8
R 1
D 1

R 1
D 1
L 1
U 1
L 1
U 1
Sample Output
Case #1: 1
Case #2: 1
Case #3: 2
Case #4: 2


你可能感兴趣的:(UVALive 7139 Rotation(模拟))