POJ_1654
由于多边形的点都是顺序走出来的,因此直接应用求多边形面积的公式即可。
#include#include #define zero 1e-8 #define MAXD 1000010 char b[MAXD]; int dx[] = { 0, -1, 0, 1, -1, 0, 1, -1, 0, 1}, dy[] = { 0, -1, -1, -1, 0, 0, 0, 1, 1, 1}; long long int det(int x1, int y1, int x2, int y2) { return (long long int)x1 * y2 - (long long int)x2 * y1; } void solve() { int i, j, k, x, y, px, py; long long int ans = 0; px = py = 0; for(i = 0; b[i] != '5'; i ++) { k = b[i] - '0'; x = px + dx[k], y = py + dy[k]; ans += det(px, py, x, y); px = x, py = y; } ans = ans < 0 ? -ans : ans; if(ans % 2) printf("%lld.5\n", ans / 2); else printf("%lld\n", ans / 2); } int main() { int t; scanf("%d", &t); while(t --) { scanf("%s", b); solve(); } return 0; }