Ultraviolet A Problem 922-rectangle by the ocean

Source: Internet
Author: User
Tags dot net
// Ultraviolet (a) problem 922-rectangle by the ocean // verdict: accepted // submission date: 2011-11-08/ultraviolet (a) Run Time: 0.028 S // All Rights Reserved (c) 2011, Qiu. Metaphysis # Yeah dot net /// [problem solving method] // This topic describes how to calculate the area of a polygon by using a directed area. Use the exhaustive method to enumerate the coordinates in the lower left corner and upper right corner of the rectangle to determine whether the rectangle has at least three corners on the polygon. Pay attention to the sequence of coordinates selected during enumeration, you can select the appropriate option to automatically obtain the // result of the minimum Lexicographic Order. # Include <iostream> # include <cmath> using namespace STD; # define maxpoly 300 struct point {int X; int y ;}; struct rect {int left, lower; int right, upper ;}; struct polygon {int N; int top, left, right, bottom; point P [maxpoly] ;}; // whether the point (I, j) is on a polygon. Int on [maxpoly] [maxpoly]; // use the exhaustive method to enumerate the coordinates in the lower left corner and upper right corner of a rectangle one by one. Void rectangle (polygon & contour, double area) {double mindiff = Area, currentdiff; rect TMP; // initialization. For (INT I = 0; I <= (contour. right-contour. left); I ++) for (Int J = 0; j <= (contour. top-contour. bottom); j ++) on [I] [J] = 0; // mark a point on a polygon as a polygon. In addition, points in the external rectangle of a polygon are not on the polygon. Note // coordinate transformation. For (INT I = 0; I <contour. n; I ++) on [contour. P [I]. x-contour. left] [contour. P [I]. y-contour. bottom] = 1; // enumerate possible rectangles. Pay attention to the enumeration order. For (INT I = 0; I <= contour. right-contour. left; I ++) for (Int J = 0; j <= contour. top-contour. bottom; j ++) for (int m = I; m <= contour. right-contour. left; m ++) for (INT n = J; n <= contour. top-contour. bottom; n ++) {// determines whether the condition of at least three corners on the polygon is met. If (on [I] [J] + on [I] [N] + on [m] [J] + on [m] [N] <3) continue; // compare the difference value. Note that it is "as close as possible" and the absolute value should be taken. Currentdiff = FABS (m-I) * (N-j)-area); If (currentdiff <mindiff) {mindiff = currentdiff; TMP = (rect) {contour. left + I, contour. bottom + J, contour. left + M, contour. bottom + N };}// output result. Cout <(INT) (area) <". "<(INT) (area * 10.0) % 10) <" "; cout <TMP. left <"" <TMP. lower <"; cout <TMP. right <"" <TMP. upper <Endl ;}// calculate the area of the polygon based on the vertex coordinates of the polygon. Double calarea (polygon & contour) {double Total = 0.0; For (INT I = 0; I <contour. n; I ++) {Int J = (I + 1) % contour. n; Total + = (contour. P [I]. x * contour. P [J]. y-contour. P [J]. x * contour. P [I]. y);} return FABS (total/2.0);} int main (int ac, char * AV []) {INT cases; polygon contour; CIN> cases; while (cases --) {CIN> contour. n; For (INT I = 0; I <contour. n; I ++) CIN> contour. P [I]. x> contour. P [I]. y; // find multiple Edge Shape external rectangular boundary. Contour. top = contour. P [0]. y; contour. left = contour. P [0]. x; contour. bottom = contour. P [0]. y; contour. right = contour. P [0]. x; For (INT I = 1; I <contour. n; I ++) {contour. top = max (contour. top, contour. P [I]. y); contour. left = min (contour. left, contour. P [I]. x); contour. bottom = min (contour. bottom, contour. P [I]. y); contour. right = max (contour. right, contour. P [I]. x);} rectangle (contour, calarea (contour);} return 0 ;}

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.