! HDU 43,803 The number of triangles with odd treasures in the house-computational geometry-(Vector fork & Segment-to-point relationship & Brute Force enumeration)

Source: Internet
Author: User

Test instructions: Xiao Ming to buy three houses, the three houses constitute a triangle, known as the coordinates of the house N, any three houses are not in a straight line, but also known as the coordinates of the M treasure, asked the house composed of triangles within the triangle has an odd number of the triangle there are many. Data range: N (3~100), M (1~1000)

Analysis:

Simple computational geometry. Remember the practice of this problem.

The number of points within the triangle = the number of points below the above segment-the number of points below the two line segments (or the following segment minus the two segments above, to see the exact position, so take the absolute value directly)

N points have n (n-1)/2 lines, not more than 1W, enumerate each segment, and then enumerate the coordinates of each treasure (10^3), to determine whether the treasure is under this line, the method is to determine whether the horizontal axis in the left and right points of the horizontal axis, and then a vector fork by <0 on the line. After processing the number of points under each line, repeat the Loop (O (N^3)) enumeration segment (The enumeration triangle) with the above formula to get the points within the triangle and determine whether it is odd.

One of the first things to do here is to sort the coordinates of the house by the first key, ordinate the second keyword, because it is convenient to enumerate.

Note that the method of finding the inner point of the triangle and the representation of the line segment (using both ends to mark Cnt[i][j])

Code:

#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include < Algorithm>using namespace Std;int n,m;struct node{long long x, y;};    Node A[200],b[1005];int cnt[200][200];bool CMP (node A,node b) {if (a.x!=b.x) return a.x<b.x; else return a.y<b.y;}    Long Long F (node A,node B,node c) {Long long ans= (b.x-a.x) * (C.Y-A.Y)-(B.Y-A.Y) * (c.x-a.x); return ans;}    int main () {int cas=1;        while (scanf ("%d%d", &n,&m)!=eof) {memset (cnt,0,sizeof (CNT));        for (int i=0;i<n;i++) scanf ("%lld%lld", &a[i].x,&a[i].y);        for (int i=0;i<m;i++) scanf ("%lld%lld", &b[i].x,&b[i].y);        Sort (a,a+n,cmp);                    for (int i=0;i<n;i++) {for (int. j=i+1;j<n;j++) {for (int k=0;k<m;k++) {                    if (b[k].x>a[i].x&&b[k].x<a[j].x) {if (f (a[i],a[j],b[k]) <0) cnt[i][j]++; }}}} int ans=0;                    for (int i=0;i<n;i++) {for (int. j=i+1;j<n;j++) {for (int k=j+1;k<n;k++) {                    int Tmp=abs (cnt[i][k]-cnt[i][j]-cnt[j][k]);                if (tmp%2==1) ans++;    }}} printf ("Case%d:%d\n", Cas++,ans); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! HDU 43,803 The number of triangles with odd treasures in the house-computational geometry-(Vector fork & Segment-to-point relationship & Brute Force enumeration)

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.