POJ 3787 convex Hull of Lattice Points seeking convex hull

Source: Internet
Author: User

Test instructions

Bare convex bag.

Analysis:

Graham template directly on.

Code:

POJ 3787//sep9#include <iostream> #include <algorithm>using namespace Std;const int maxn=64;struct p{int x , Y;} Pnt[maxn],cnt[maxn];int n;int cmp (P a,p b) {if (A.Y!=B.Y) return A.y<b.y;return a.x<b.x;} int Cross (P a,p b,p c) {int x1=b.x-a.x;int y1=b.y-a.y;int x2=c.x-a.x;int y2=c.y-a.y;return x1*y2-x2*y1;} int Graham () {sort (pnt,pnt+n,cmp); int i,pos=1;cnt[0]=pnt[0];cnt[1]=pnt[1];for (i=2;i<n;++i) {while (pos>0& &cross (Cnt[pos-1],cnt[pos],pnt[i]) <=0)--pos;cnt[++pos]=pnt[i];} int Bak=pos;for (i=n-2;i>=0;--i) {while (Pos>bak&&cross (Cnt[pos-1],cnt[pos],pnt[i]) <=0)--pos;cnt[ ++pos]=pnt[i];} return POS;} int main () {int cases,case_num;scanf ("%d", &cases), while (cases--) {scanf ("%d%d", &case_num,&n); int pos; for (int i=0;i<n;++i) scanf ("%d%d", &pnt[i].x,&pnt[i].y);p rintf ("%d%d\n", Case_num,pos=graham ()); int index=0;for (int i=1;i<pos;++i) if (CNT[I].Y&GT;CNT[INDEX].Y) index=i;else if (cnt[i].y==cnt[index].y&&cnt [I].x<cnt[index].x] index=I;int Mod=pos;while (pos--) {printf ("%d%d\n", cnt[index].x,cnt[index].y);--index;index+=mod;index%=mod;}}  return 0;}


POJ 3787 convex Hull of Lattice Points seeking convex hull

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.