[Convex packet Graham_Scan algorithm] HDU 1348 Wall

Source: Internet
Author: User

Typical convex bag question: Calculate the perimeter of the peripheral wall

 

C ++ code

# Include <iostream>

# Include <fstream>

# Include <algorithm>

# Include <string>

# Include <set>

// # Include <map>

# Include <queue>

# Include <utility>

# Include <stack>

# Include <list>

# Include <vector>

# Include <cstdio>

# Include <cstdlib>

# Include <cstring>

# Include <cmath>

# Include <ctime>

# Include <ctype. h>

Using namespace std;

# Define PI 3.14159265

 

Struct point {

Double x, y, angel;

} P [1, 1005], ch [2, 1005];

 

Double dist (point a, point B)

{

Return sqrt (a. x-b.x) * (a. x-b.x) + (a. y-b.y) * (a. y-b.y ));

}

 

Double multi (point a, point B, point c)

{

Double x1, y1, x2, y2;

X1 = B. x-a. x;

Y1 = B. y-a. y;

X2 = c. x-B. x;

Y2 = c. y-B. y;

Return x1 * y2-x2 * y1;

}

 

Bool cmp (point a, point B)

{

If (a. y = B. y)

Return a. x <B. x;

Return a. y <B. y;

}

 

Bool cmp2 (point a, point B)

{

If (a. angel = B. angel)

{

If (a. x = B. x)

Return a. y> B. y;

Return a. x> B. x;

}

Return a. angel <B. angel;

}

 

Int main ()

{

Int n, I, top, t;

Double r, len;

Scanf ("% d", & t );

While (t --)

{

Scanf ("% d % lf", & n, & r );

For (I = 0; I <n; I ++)

Scanf ("% lf", & p [I]. x, & p [I]. y );

Sort (p, p + n, cmp); // locate p [0] in the lower left corner.

 

// Locate the polar angle relative to p [0] and sort the vertices except p [0] According to the Polar Angle

For (I = 1; I <n; I ++)

P [I]. angel = atan2 (p [I]. y-p [0]. y, p [I]. x-p [0]. x );

Sort (p + 1, p + n, cmp2 );

 

// Graham_Scan Algorithm

Ch [0] = p [0], ch [1] = p [1], ch [2] = p [2];

Top = 3;

For (I = 3; I <n; I ++)

{

While (top> 2 & multi (ch [top-2], ch [top-1], p [I]) <= 0)

Top --;

Ch [top ++] = p [I];

}

 

// Calculate the perimeter

Len = dist (ch [0], ch [top-1]);

For (I = 1; I <top; I ++)

Len + = dist (ch [I], ch [I-1]);

Len + = 2 * PI * r; // Add an arc to the circle!

 

Printf ("%. 0lf \ n", len );

If (t)

Printf ("\ n ");

}

Return 0;

}

Related Article

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.