Car 多边形面积

链接:https://ac.nowcoder.com/acm/contest/560/K
来源:牛客网
 

题目描述

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(1≤N≤106)

题解:模拟一下,把他能到达的点全都算出来,然后公式算一下即可;

多边形面积公式:S=(x1y2-x2y1)/2+(x2y3-x3y2)/2+...+(xny1-x1yn)/2

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
double a[N*5][2];
int len;
int n;
int m;
double x,y;
void add(double z)
{
	if(m==0)
	{
		y+=z;
	}
	if(m==1)
	{
		x+=z;
	}
	if(m==2)
	{
		y-=z;
	}
	if(m==3)
	{
		x-=z;
	}
//	cout<<x<<" "<<y<<endl;
	len++;
	a[len][0]=x;
	a[len][1]=y;
}
int main()
{
	char op[2];
	scanf("%d",&n);
	m=0;
	a[len][0]=0,a[len][1]=0;
	double xx,yy;
	for(int i=1;i<=n;i++)
	{
		scanf("%s",op);
		if(op[0]=='A')
		{
			scanf("%lf%lf",&xx,&yy);
			add(xx);
			m=(m+1+4)%4;
			add(yy);
			m=(m+1)%4;
		}
		if(op[0]=='B')
		{
			scanf("%lf%lf",&xx,&yy);
			add(xx);
			m=(m-1+4)%4;
			add(yy);
			m=(m-1+4)%4;
		}
		if(op[0]=='C')
		{
			scanf("%lf%lf",&xx,&yy);
			add(xx);
			m=(m+1)%4;
			add(yy);
			m=(m+1)%4;
			scanf("%lf%lf",&xx,&yy);
			add(xx);
			m=(m+1)%4;
			add(yy);
			m=(m+1)%4;
		}
	}
	len++;
	a[len][0]=0;a[len][1]=0;
	double ans=0;
	for(int i=0;i<len;i++)
	{
		ans+=(a[i][0]*a[i+1][1]-a[i+1][0]*a[i][1]);
	}
    	
    ans=ans/2;
    printf("%.0f\n",fabs(ans));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/89284166
car
今日推荐