题解:CF183A Headquarters

推一下蒟蒻的博客

题面

Headquarters

题面翻译

警方要抓捕一名逃犯,逃犯开着车从总部驶出,撞上了一个冰淇淋摊,摊位、汽车和总部在二维平面上各占据一个点。

车在路上正好做了n次移动,每次按 L、R、D、U ⁡ \operatorname{L、R、D、U} LRDU 分别可以左、右、上、下移动一格

显示的移动不准确,它无法保存汽车运动的确切顺序。相反,它会记录汽车可能的运动。每个记录都是以下类型之一的字符串: UL、UR、DL、DR ⁡ \operatorname{UL、UR、DL、DR} ULURDLDR 或者 ULDR ⁡ \operatorname{ULDR} ULDR 每个这样的字符串意味着汽车做出了与字符串中的一个对应的移动。

你收到了逃犯可能的行动。记录是按时间顺序排列的,冰淇淋摊位于 ( 0 , 0 ) (0,0) (0,0) 你的任务是输出总部的不同点的数量

题目描述

Sensation, sensation in the two-dimensional kingdom! The police have caught a highly dangerous outlaw, member of the notorious “Pihters” gang. The law department states that the outlaw was driving from the gang’s headquarters in his car when he crashed into an ice cream stall. The stall, the car, and the headquarters each occupies exactly one point on the two-dimensional kingdom.

The outlaw’s car was equipped with a GPS transmitter. The transmitter showed that the car made exactly $ n $ movements on its way from the headquarters to the stall. A movement can move the car from point ( x , y ) (x,y) (x,y) to one of these four points: to point ( x − 1 , y ) (x-1,y) (x1,y) which we will mark by letter “L”, to point ( x + 1 , y ) (x+1,y) (x+1,y) — “R”, to point ( x , y − 1 ) (x,y-1) (x,y1) — “D”, to point ( x , y + 1 ) (x,y+1) (x,y+1) — “U”.

The GPS transmitter is very inaccurate and it doesn’t preserve the exact sequence of the car’s movements. Instead, it keeps records of the car’s possible movements. Each record is a string of one of these types: “UL”, “UR”, “DL”, “DR” or “ULDR”. Each such string means that the car made a single movement corresponding to one of the characters of the string. For example, string “UL” means that the car moved either “U”, or “L”.

You’ve received the journal with the outlaw’s possible movements from the headquarters to the stall. The journal records are given in a chronological order. Given that the ice-cream stall is located at point ( 0 , 0 ) (0,0) (0,0) , your task is to print the number of different points that can contain the gang headquarters (that is, the number of different possible locations of the car’s origin).

输入格式

The first line contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) (1 \le n \le 2·10^{5}) (1n2105) — the number of the car’s movements from the headquarters to the stall.

Each of the following n n n lines describes the car’s possible movements. It is guaranteed that each possible movement is one of the following strings: “UL”, “UR”, “DL”, “DR” or “ULDR”.

All movements are given in chronological order.

Please do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin and cout stream or the %I64d specifier.

输出格式

Print a single integer — the number of different possible locations of the gang’s headquarters.

样例 #1

样例输入 #1

3
UR
UL
ULDR
样例输出 #1
9

样例 #2

样例输入 #2
2
DR
DL
样例输出 #2
4

提示

The figure below shows the nine possible positions of the gang headquarters from the first sample:

For example, the following movements can get the car from point $ (1,0) $ to point $ (0,0) $ :

题解

思路

假设冰淇凌摊原来的位置在 ( x , y ) \left ( x,y \right ) (x,y),那么任意一次运动可能的位置在 ( x − 1 , y − 1 ) \left ( x-1,y-1 \right ) (x1,y1) ( x − 1 , y + 1 ) \left ( x-1,y+1 \right ) (x1,y+1) ( x , y ) \left ( x,y \right ) (x,y) ( x + 1 , y − 1 ) \left ( x+1,y-1 \right ) (x+1,y1) ( x + 1 , y + 1 ) \left ( x+1,y+1 \right ) (x+1,y+1),如下图所示。

所以,一共有 6 6 6 种方法走到 A ( x , y ) A\left ( x,y \right ) A(x,y)

  1. B ( x − 1 , y − 1 ) B\left ( x-1,y-1 \right ) B(x1,y1) 选择 U R UR UR
  2. C ( x − 1 , y + 1 ) C\left ( x-1,y+1 \right ) C(x1,y+1) 选择 U L UL UL
  3. D ( x + 1 , y − 1 ) D\left ( x+1,y-1 \right ) D(x+1,y1) 选择 D R DR DR
  4. E ( x + 1 , y + 1 ) E\left ( x+1,y+1 \right ) E(x+1,y+1) 选择 D L DL DL
  5. A ( x , y ) A\left ( x,y \right ) A(x,y) 选择 U L D R ULDR ULDR
  6. A ( x , y ) A\left ( x,y \right ) A(x,y) 不进行选择。

所以我们就应当用 map<string,int> 存下来每一种走的类型的步数。

冰淇凌摊的移动路线如下图所示。

因为第 5 5 5 种方案和第 6 6 6 种方案同时可以从直线 f f f 和直线 g g g 走到 $A\left ( x,y \right ) $,所以它们应同时计入到直线 f f f 和直线 g g g 之中。

所以从直线 f f f 上走的方案数是 c n t U L + c n t D R + c n t U L D R + 1 cnt_{UL}+cnt_{DR}+cnt_{ULDR}+1 cntUL+cntDR+cntULDR+1,从直线 g g g 上走的方案数是 c n t U R + c n t D L + c n t U L D R + 1 cnt_{UR}+cnt_{DL}+cnt_{ULDR}+1 cntUR+cntDL+cntULDR+1。因此最终的答案就是 ( c n t U L + c n t D R + c n t U L D R + 1 ) × ( c n t U R + c n t D L + c n t U L D R + 1 ) \left ( cnt_{UL}+cnt_{DR}+cnt_{ULDR}+1 \right ) \times \left ( cnt_{UR}+cnt_{DL}+cnt_{ULDR}+1 \right ) (cntUL+cntDR+cntULDR+1)×(cntUR+cntDL+cntULDR+1)

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
string s;
map<string, int> cnt;
signed main() {
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i  \le  n; i++) {
		cin >> s;
		cnt[s]++;
	}
	cout << (cnt["UR"] + cnt["DL"] + cnt["ULDR"] + 1)*(cnt["UL"] + cnt["DR"] + cnt["ULDR"] + 1);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值