UVALive 7139 Rotation(模拟)

本文介绍了一个计算二维网格中路径经过后每个单元格旋转值的方法,通过跟踪路径的变化并利用差分思想更新每个单元格的旋转值,最终计算出所有单元格旋转值的平方和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


#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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值