【POJ1654】Area【叉积】

版权声明:若希望转载,在评论里直接说明即可,谢谢! https://blog.csdn.net/SSL_ZYC/article/details/85803106

题目大意:

题目链接:http://poj.org/problem?id=1654
根据特殊的读入方式得到一个多边形,求这个多边形的面积。


思路:

建议先看下文注意事项,谢谢!
求平面直角坐标系中多边形面积模板题。
设上一个顶点的坐标为 ( x 1 , y 1 ) (x1,y1) ,这一个顶点的面积坐标为 ( x 2 , y 2 ) (x2,y2) 。那么以 ( 0 , 0 ) , ( x 1 , y 1 ) , ( x 2 , y 2 ) (0,0),(x1,y1),(x2,y2) 三点组成的三角形面积就是 ( x 1 , y 1 ) , ( x 2 , y 2 ) (x1,y1),(x2,y2) 两点的叉积的一半。
根据容斥原理,易得该多边形面积就是所有三角形的面积和的绝对值。


代码:

#include <cstdio>
using namespace std;
typedef long long ll;

const int N=1000010;
const ll dx[]={0,1,1,1,0,0,0,-1,-1,-1};
const ll dy[]={0,-1,0,1,-1,0,1,-1,0,1};
int Q,i;
ll ans,x1,x2,y1,y2;

int main()
{
	scanf("%d",&Q);
	while (Q--)
	{
		x1=y1=ans=0;
		while (scanf("%1d",&i)==1&&i!=5)
		{
			x2=x1+dx[i];
			y2=y1+dy[i];
			ans+=(x1*y2-x2*y1);  //叉积
			x1=x2;
			y1=y2;
		}
		if (ans<0) ans=-ans;
		if (ans&1) printf("%lld.5\n",ans/2);
			else printf("%lld\n",ans/2);
	}
	return 0;
}

注意事项

  • 首先 G C C GCC c e ce G + + G++ w a wa ,不要用这两个编译器。可以使用 C + + C++

  • 最终答案是要取绝对值,但 f a b s fabs c + + c++ 居然会 c e ce ,所以要手写 a b s abs

  • 因为答案一定是整数或整数除以二,所以用整形存,不能用 f l o a t float d o u b l e double

  • 还有答案可能爆 i n t int ,所以要开 l o n g   l o n g long\ long

  • 最坑的是不能所有的变量都开,会 M L E MLE ,所以只开跟答案相关的就行。

摘自本题讨论,同时感谢讨论的第一点和第二点救了我。

猜你喜欢

转载自blog.csdn.net/SSL_ZYC/article/details/85803106