Title Link: http://acm.swust.edu.cn/problem/567/
Time limit (ms): 655,351 Tigers since the < jailbreak >, temper has become more irritable, and became the Secret of God and God. One day the administrator found that the tiger disappeared, this he can be nasty, hurriedly informed 110. Fortunately The tiger is fitted with a GPS so there is hope of finding him.
Your task is to let the computer calculate the tiger's position by GPs-given the coordinates of the tiger and the coordinates of the cage, to hide in the hole, or to run outside. Description input data has n+2 line:
The first line is a data N (0<=N<=12), which represents the number of coordinates x, y of the cage (x, y are within the int range);
The second to n+1 rows are n coordinates x, y, followed by a counterclockwise order.
The last line is the tiger's coordinates x, y;
Note: Only one set of test data, the input is an integer, the cage must be a convex polygon, and the tiger will not appear on the cage boundary
The area of the triangle can be calculated using the outer product of the vector, then the polygon is n triangles, then you can ...
Input if the tiger is still in the cage, enter "yes", otherwise "NO"; Output
123456789101112131415 |
40 01 01 10 10.5 0.5 61-12 01 1-1 1-2 0-1-13 3 |
Sample Input
Sample OutputThinking of solving problems: This problem may seem to be a very difficult math problem, but carefully read the question will find that the problem is actually compared to the size of the convex hull of the cage, and add the new point set of tiger coordinates the area of the new convex hull is equal to the problem, equal to the tiger in the cage, and vice versa ~ ~ about convex hull, you can poke Over here:http://www.cnblogs.com/zYx-ac/p/4540984.html
The code is as follows:
1 //only to determine the convex hull area of the point, and the cage form convex hull area is equal to2 3#include <iostream>4#include <cstdio>5#include <algorithm>6#include <stack>7#include <cmath>8 using namespacestd;9 structnode{Ten Doublex, y; One}point[ the], tiger[101], PosA, PosB, TMP;//POSB Tiger Base Amount A - DoubleDis (Node A, Node B) { - returnPow ((A.X-B.Y),2) + POW ((A.Y-B.Y),2); the } - - //sorting by point set distribution and judging by vector parallel relation - BOOLCMP (Node A, Node B) { + DoublePovit = (a.x-posa.x) * (B.Y-POSA.Y)-(b.x-posa.x) * (A.Y-posa.y); - if(Povit >0|| !povit && (Dis (A, PosA) <Dis (b, PosA ))) + return true; A return false; at } - - //whether the current point is on the left side of the point set, compares the slope relationship of 3 points and two lines using a fork multiply - BOOLturn_left (node P1, node P2, node p3) { - return(p2.x*p1.y + p3.x*p2.y + p1.x*p3.y-p3.x*p1.y-p1.x*p2.y-p2.x*p3.y) >0?true:false; - } in - intMain () { to intI, Sign1, SIGN2, N; + Doublearea1, area2; -CIN >>N; the for(i =0; I < n; i++){ *CIN >> point[i].x >>point[i].y; $tiger[i].x =point[i].x;Panax NotoginsengTIGER[I].Y =point[i].y; - } theCIN >> tiger[i].x >> tiger[i].y;//Tiger coordinates +Stack<node>Q; ASIGN1 =0; thePosA = PosB = point[0]; + //find the bottom left point of the cage and add the point set of the tiger coordinates separately - for(i =1; I < n +1; i++){ $ if(i<N) { $ if(posa.y = = point[i].y&&posa.x>point[i].x | | Point[i].y <posa.y) { -PosA =Point[i]; -SIGN1 =i; the } - }Wuyi Else{ thePosB =PosA; -SIGN2 =sign1; Wu if(posb.y = = tiger[i].y&&posb.x>tiger[i].x | | Tiger[i].y <posb.y) { -PosB =Tiger[i]; AboutSIGN2 =i; $ } - } - } -Swap (point[0], point[sign1]); ASwap (tiger[0], tiger[sign2]); +Sort (Point +1, point + N, CMP);//it is convenient to filter the points that make up the convex hull of a graph by using the discretization of the fork-by-point set . theSort (Tiger +1, Tiger + n +1, CMP); -Q.push (tiger[0]), Q.push (tiger[1]), Q.push (tiger[2]); $ the for(i =3; I < n +1; i++){ the while(!Q.empty ()) { theTMP =q.top (); the Q.pop (); - if(Turn_left (TMP, Q.top (), Tiger[i])) { in Q.push (TMP); the Break; the } About } the Q.push (Tiger[i]); the } the //because the cage must be convex, the area is calculated directly +area1 = AREA2 =0; -TMP = Point[n-1]; theArea1 + = (posa.x*tmp.y-posa.y*tmp.x)/2.0;Bayi for(i = n-2; I >=0; i--){ theArea1 + = (tmp.x*point[i].y-tmp.y*point[i].x)/2.0; theTMP =Point[i]; - } - theTMP =q.top (), Q.pop (); theAREA2 + = (posa.x*tmp.y-posa.y*tmp.x)/2.0; the while(!Q.empty ()) { theAREA2 + = (tmp.x*q.top (). Y-tmp.y*q.top (). x)/2.0; -TMP =q.top (); the Q.pop (); the } theprintf"%s\n", area1 = = area2?"YES":"NO");94 return 0; the}
View Code
Swust OJ 567--Tiger is not in the cage (convex hull problem)