NC24804. Car
描述
First, there is a car whose coordinate is (0, 0) and it’s facing the positive direction of Y-axis. Then it is given a sequence of instructions which it should act one by one. The instructions are classified into three and only three types:
Type A: Go forward x meters and turn right, then go forward y meters and turn right.
Type B: Go forward x meters and turn left, then go forward y meters and turn left.
Type C: Go forward x meters and turn right, then go forward y meters and turn right, next go forward z meters and turn right, finally go forward w meters and turn right.
And it is guaranteed:
1. While acting the instructions, the car’s x-coordinate will be always inside [0, 230) and its y-coordinate will be always inside (-230, 230).
2. Two adjacent instructions won't be of the same type.
3. The instruction of type C will be given only one time in the sequence.
4. The car will be back to the starting point when it finishes acting all instructions in the sequence.
5. The car’s path has no intersection.
Please calculate the area of the polygon surrounded by the path the car passes by acting all instructions in the order.
输入描述
The first line contains the integer N,the numbers of instructions in the sequence.Next N lines describe the instructions in the sequence.For each line, you are given an upper-case letter which means the type of instruction. “A” means type A, “B” means type B and “C” means type C.
If you are given an instruction of type A or B, next you will be given two integers x and y. If you are given an instruction of type C, you will be given four integers x, y, z and w.
输出描述
Only one integer means the area of the polygon surrounded by the path the car passes by acting all instructions in order.
示例1
输入:
3 A 100 60 B 30 50 C 90 700 160 810
输出:
121500
C++14(g++5.4) 解法, 执行用时: 259ms, 内存消耗: 504K, 提交时间: 2019-04-13 12:22:46
#include<stdio.h> #include<string.h> typedef long long ll; ll re=0; ll f=1; ll bx=0; ll by=0; void aa(ll x,ll y){ if(f==0){ bx+=x; by+=y; re+=(x*by); } else{ bx-=x; by-=y; re+=((-x)*by); } } void bb(ll x,ll y){ if(f==0){ bx-=x; by+=y; re+=((-x)*by); } else{ bx+=x; by-=y; re+=(x*by); } } int main () { ll n; scanf("%lld",&n); for(ll i=0;i<n;i++){ char a; getchar(); scanf("%c",&a); ll x,y; if(a=='A'){ f=(f+1)%2; scanf("%lld%lld",&y,&x); aa(x,y); }else if(a=='B'){ f=(f+1)%2; scanf("%lld%lld",&y,&x); bb(x,y); } else{ f=(f+1)%2; scanf("%lld%lld",&y,&x); aa(x,y); f=(f+1)%2; scanf("%lld%lld",&y,&x); aa(x,y); } } printf("%lld\n",re); }
C++11(clang++ 3.9) 解法, 执行用时: 360ms, 内存消耗: 508K, 提交时间: 2020-02-26 19:10:16
#include<bits/stdc++.h> #define LL long long using namespace std; int x,y,z,r,n; LL pos,f=1,d=1; LL area; char s[5]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",s); if(s[0]=='C') { scanf("%d%d%d%d",&x,&y,&z,&r); pos+=d*x; area+=pos*y*f; f=-f; d=-d; pos+=d*z; area+=pos*r*f; d=-d; } else { scanf("%d%d",&x,&y); pos+=d*x; area+=pos*y*f; d=-d; } } printf("%lld\n",area); return 0; }