0
点赞
收藏
分享

微信扫一扫

【poj1654】Area【叉积】

mm_tang 2022-01-18 阅读 71

题目大意

一个坐标系,从原点开始走,然后1-4分别代表,向右下走,向右走,向右上走,向下走,5代表回到原点,6-9代表,向上走,向左下走,向左走,向左上走。(看翻译直接无限WA),给出一串包含1-9的字符串,问你这些点所围成的面积

分析

因为输入的都是相邻的点,我们就每次记录一条线段两端的坐标,把这个三角形算出来(叉积/2)。最后取绝对值就可以

上代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

int t;
char s[1000010];

long long cj(int x1,int y1,int x2,int y2)
{
	return x1*y2-x2*y1;
}

int main()
{
	cin>>t;
	while(t--)
	{
		scanf("%s",s);
		int x=0,y=0,n=strlen(s);
		long long ans=0;
		for(register int i=1;i<=n;i++)
		{
			int x1=x,y1=y;
			if(s[i]=='1') x1++,y1--;
			if(s[i]=='2') x1++;
			if(s[i]=='3') x1++,y1++;
			if(s[i]=='4') y1--;
			if(s[i]=='5'
			) x1=0,y1=0;
			if(s[i]=='6') y1++;
			if(s[i]=='7') x1--,y1--;
			if(s[i]=='8') x1--;
			if(s[i]=='9') x1--,y1++;
			ans+=cj(x,y,x1,y1);
			x=x1,y=y1;
		}
		if(ans<0) ans=-ans;
		if(ans%2==0) cout<<ans/2<<endl;
		else cout<<ans/2<<".5"<<endl;
	}
	return 0;
}
举报

相关推荐

0 条评论