poj 1654 這題我想多了

來源:互聯網
上載者:User

看到本題目簡單分析了一下

只要解決兩個問題就能搞定這一題。

1、面積為0的情況,分兩種,一種是一條直線,一種是折線走出去,在原路返回

2、求出所有點座標,求手繪多邊形的面積

這兩個問題都很容易解決。

1、直線情況也就兩個對角線,和水平垂直。原路返回的可以將反向的標示當做是一樣的,比如2和8都當做8,如果這個字串是迴文,那麼就是原路返回

2、手繪多邊形面積,這個直接求相鄰點的叉積就可以了。實際上就是根據向量面積來計算的,這個這裡不多做解釋,搜一下求手繪多邊形面積,網上很多解釋,大家可以看看。

 

後來我發現我想多了,第一種情況就是第二種的特殊情況,沒必要單獨列出來,悲劇啊。

另外注意的是必須要用long long儲存結果,因為結果數很大。還有一點疑惑,沒看出來題目哪裡說北是向下的,我們都是上北下南左西右東,這裡剛好反了。。。。

 

理解了上面的解釋,代碼就很簡單了。水題不水啊。。。

 

#include <stdio.h>#include <math.h>#include <stdlib.h>#include <string.h>__int64 func(__int64 x1, __int64 y1, __int64 x2, __int64 y2){return (x1 * y2 - x2 * y1);}int main(){char ch[1000001];__int64 x1, y1, x2, y2, ans = 0;int d[10][2] =  {{0,0},{1,-1},{1,0},{1,1},{0,-1},{0,0},{0,1},{-1,-1},{-1,0},{-1,1}};int n, i,j,p;scanf("%d", &n);while (n --){scanf("%s", ch);ans = 0;x1 = y1 = 0;j = strlen(ch);for (i = 0; i < j - 1; ++ i){p = ch[i] - '0';x2 = x1 + d[p][0];y2 = y1 + d[p][1];ans += func(x1, y1, x2, y2);x1 = x2;y1 = y2;}if (ans < 0){ans = 0 - ans;}if (ans % 2){printf("%I64d.5\n", ans / 2);}elseprintf("%I64d\n", ans / 2);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.