【代碼超詳解】POJ 1265 Area(簡單計算幾何 · Pick 定理)

一、傳送門

http://poj.org/problem?id=1265

二、算法分析說明與代碼編寫指導

一開始機器人在原點(題目好像沒明說),然後每次給出移動的相對座標,最後圍成一個簡單多邊形,問這個多邊形的面積是多少。確保移動總是逆時針的,而且多邊形爲簡單多邊形。
已知:
在這裏插入圖片描述
在這裏插入圖片描述在這裏插入圖片描述(上圖截取自oi-wiki)

三、AC 代碼

#include<cstdio>
#include<cmath>
#pragma warning(disable:4996)
struct point { int x, y; };
const unsigned nmax = 101;
unsigned t, n; point p[nmax]; int dx, dy, A, E;
template<class _Ty> _Ty gcd(const _Ty& a, const _Ty& b) { return b == 0 ? a : gcd(b, a % b); }//a, b > 0
inline int area_2x(const point& a, const point& b) { return a.x * b.y - a.y * b.x; }
int main() {
	scanf("%u", &t);
	for (unsigned h = 1; h <= t; ++h) {
		scanf("%u", &n); E = 0; A = 0;
		for (unsigned i = 1; i <= n; ++i) {
			scanf("%d%d", &dx, &dy); p[i].x = p[i - 1].x + dx; p[i].y = p[i - 1].y + dy;
			E += gcd(abs(dx), abs(dy)); A += area_2x(p[i - 1], p[i]);
		}
		printf("Scenario #%u:\n%d %d %.1lf\n\n", h, (A - E + 2) / 2, E, A * 0.5);
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章