When you were five years old, you watched in horror as a spiked devil murdered your parents. You would have died too, except you were saved by Rose, a passing demon hunter. She ended up adopting you and training you as her apprentice.
Rose’s current quarry is a clock devil which has been wreaking havoc on the otherwise quiet and unassuming town of Innsmouth. It comes out each night to damage goods, deface signs, and kill anyone foolish enough to wander around too late. The clock devil has offed the last demon hunter after it; due to its time-warping powers, it is incredibly agile and fares well in straight-up fights.
The two of you have spent weeks searching through dusty tomes for a way to defeat this evil. Eventually, you stumbled upon a relevant passage. It detailed how a priest managed to ensnare a clock devil by constructing a trap from silver, lavender, pewter, and clockwork. The finished trap contained several pieces, which must be placed one-by-one in the shape of a particular polygon, in counter-clockwise order. The book stated that the counter-clockwise order was important to counter the clock devil’s ability to speed its own time up, and that a clockwise order would only serve to enhance its speed.
It was your responsibility to build and deploy the trap, while Rose prepared for the ensuing fight. You carefully reconstruct each piece as well as you can from the book. Unfortunately, things did not go as planned that night. Before you can finish preparing the trap, the clock devil finds the two of you. Rose tries to fight the devil, but is losing quickly. However, she is buying you the time to finish the trap. You quickly walk around them in the shape of the polygon, placing each piece in the correct position. You hurriedly activate the trap as Rose is knocked out. Just then, you remember the book’s warning. What should you do next?
Rose’s current quarry is a clock devil which has been wreaking havoc on the otherwise quiet and unassuming town of Innsmouth. It comes out each night to damage goods, deface signs, and kill anyone foolish enough to wander around too late. The clock devil has offed the last demon hunter after it; due to its time-warping powers, it is incredibly agile and fares well in straight-up fights.
The two of you have spent weeks searching through dusty tomes for a way to defeat this evil. Eventually, you stumbled upon a relevant passage. It detailed how a priest managed to ensnare a clock devil by constructing a trap from silver, lavender, pewter, and clockwork. The finished trap contained several pieces, which must be placed one-by-one in the shape of a particular polygon, in counter-clockwise order. The book stated that the counter-clockwise order was important to counter the clock devil’s ability to speed its own time up, and that a clockwise order would only serve to enhance its speed.
It was your responsibility to build and deploy the trap, while Rose prepared for the ensuing fight. You carefully reconstruct each piece as well as you can from the book. Unfortunately, things did not go as planned that night. Before you can finish preparing the trap, the clock devil finds the two of you. Rose tries to fight the devil, but is losing quickly. However, she is buying you the time to finish the trap. You quickly walk around them in the shape of the polygon, placing each piece in the correct position. You hurriedly activate the trap as Rose is knocked out. Just then, you remember the book’s warning. What should you do next?
输入
The first line of input contains a single integer T (1 ≤ T ≤ 100), the number of test cases. The first line of each test case contains a single integer n (3 ≤ n ≤ 20), the number of pieces in the trap. Each of the next n lines contains two integers xi and yi (|xi |, |yi | ≤ 100), denoting the x and y coordinates of where the ith piece was placed. It is guaranteed that the polygon is simple; edges only intersect at vertices, exactly two edges meet at each vertex, and all vertices are distinct.
输出
For each test case, output a single line containing either fight if the trap was made correctly or run if the trap was made incorrectly.
样例输入
2
3
0 0
1 0
0 1
3
0 0
0 1
1 0
样例输出
fight run
题大意就是判断按顺序给出来的n个点是顺时针还是逆时针
(因为题目说过两条边只会在顶点,因此按顺序连起所有边肯定是是一个闭合多边形
故不是顺时针就是逆时针)
比赛时还不知道Green公式
所以只能硬着头皮做下去,我是把所有点的x取平均值,y也取平均值,这个点(xx,yy)作为中心点
然后把所有n个点和中心点连接
然后我们每条边都求出一个角度(0-2pi),我们可以发现,如果是逆时针的话,那么下一条边的角度肯定比上一条大(有一条是比前一条小的),
相反顺时针就会是小(有一条是比前一条大的),
因此我们判断一下有多少是比前一条角度大的 sumx,
有多少是比前一条小的sumy,
如果sumx>=sumy
就是逆时针
#include<bits/stdc++.h> using namespace std; const int maxn=500+10; const double pi=acos(-1); typedef struct { double x,y; double angle; }qq; qq a[maxn]; double an(double xx,double yy) { if(xx>=0&&yy>=0) { return atan(yy/xx); } else if(xx<0&&yy>=0) { return pi+atan(yy/xx); } else if(xx<=0&&yy<=0) { return pi+atan(yy/xx); } else { return 2.0*pi+atan(yy/xx); } } int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); double sumx=0; double sumy=0; for(int i=0;i<n;i++) { scanf("%lf%lf",&a[i].x,&a[i].y); sumx+=a[i].x; sumy+=a[i].y; } sumx/=n; sumy/=n; for(int i=0;i<n;i++) { double xx=a[i].x-sumx; double yy=a[i].y-sumy; a[i].angle=an(xx,yy); } int sum=0; for(int i=0;i<n;i++) { if(a[i].angle<a[(i-1+n)%n].angle) { sum++; } } if(sum<=(n-sum)) { printf("fight\n"); } else { printf("run\n"); } } return 0; }
另一种方法就是用Green 定理
https://blog.csdn.net/henuyh/article/details/80378818
#include<bits/stdc++.h> using namespace std; double x[1050],y[1050]; int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); double temp = 0; for(int i=0;i<n;i++) { scanf("%lf%lf", &x[i], &y[i]); } for(int i=0;i<n-1;i++) { temp += -0.5 * (y[i + 1] + y[i]) * (x[i + 1] - x[i]); } if(temp > 0) printf("fight\n"); else printf("run\n"); } return 0; }