ZOJ 2157 Building a New Depot(POJ 1788)

來源:互聯網
上載者:User

感覺這就不是計算幾何題,是排序題……

 

做兩次二級排序,關鍵字是點的x,y座標值。x優先排序時,計算共線的點 y 的差距絕對值之和,就是所有豎直(南北向)圍牆長度;y優先排序時,計算共線點 x 的差距絕對值之和,就是所有橫著(東西向)的圍牆長度。兩個加起來輸出就好。

 

ps:注意凹多形的情況,下面給幾組資料就是凹的情況

 

 

12

2 0

3 0

0 1

2 1

0 2

1 2

0 3

1 3

2 3

3 3

0 4

2 4

 

 

The length of the fence will be 16 units.

 

 

81 21 32 12 23 13 24 24 3The length of the fence will be 10 units.

 

代碼:

#include<stdio.h><br />#include<math.h><br />#include<stdlib.h><br />#define N 14<br />struct point<br />{<br />int x,y;<br />}p[N];<br />int cmp1(const void *a,const void *b)<br />{<br />struct point *aa=(point *)a;<br />struct point *bb=(point *)b;<br />if(aa->x==bb->x)return aa->y > bb->y ?1:-1;<br />else return aa->x > bb->x ?1:-1;<br />}<br />int cmp2(const void *a,const void *b)<br />{<br />struct point *aa=(struct point *)a;<br />struct point *bb=(struct point *)b;<br />if(aa->y==bb->y)return aa->x > bb->x ?1:-1;<br />else return aa->y > bb->y ?1:-1;<br />}<br />int main()<br />{<br />int i,s,n;<br />while(scanf("%d",&n),n)<br />{<br />for(i=0;i<n;i++)<br />scanf("%d%d",&p[i].x,&p[i].y);<br />s=0;<br />qsort(p,n,sizeof(p[0]),cmp1);<br />for(i=0;i<n-1;)<br />{<br />if(p[i].x==p[i+1].x)<br />{<br />s+=abs(p[i].y-p[i+1].y);<br />i=i+2;<br />}<br />else i++;<br />}<br />qsort(p,n,sizeof(p[0]),cmp2);<br />for(i=0;i<n-1;)<br />{<br />if(p[i].y==p[i+1].y)<br />{<br />s+=abs(p[i].x-p[i+1].x);<br />i=i+2;<br />}<br />else i++;<br />}<br />printf("The length of the fence will be %d units./n",s);</p><p>}<br />return 0;<br />} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.