Hunter’s Apprentice(判断所走路线为顺时针或逆时针)【Green公式】
Hunter’s Apprentice
题目描述
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?输入
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
提示
In the first case, you went around the polygon in the correct direction, so it is safe to fight the clock devil
and try to save Rose.
In the second case, you messed up, and it is time to start running. Sorry Rose!
思路:
给出一系列点的坐标 每两个点表示一条线 最后判断所走路线是顺时针还是逆时针 逆时针输出fight 顺时针输出run
开始想了个错误的思路 主要是通过三点之间的斜率关系判断 但忽略了k=MAX 或者是 k=-MAX 导致错误
可能还有其他的办法做 这是一种:
green公式:https://baike.baidu.com/item/%E6%A0%BC%E6%9E%97%E5%85%AC%E5%BC%8F/4542338(百度)
推导过程:https://blog.csdn.net/qq_37602930/article/details/80496498
c代码如下:
double d=0;
for(int i=0;i<n-1;i++){
d+=-0.5*(edge[i+1].y+edge[i].y)*(edge[i+1].x-edge[i].x);
}
if(d<0){ ///d<0为顺时针
printf("run\n");
}
else{
printf("fight\n");
}
}
AC代码:
#include<stdio.h>
const int MAX=2e2;
const int MAX1=2e5;
struct node{
double x;
double y;
}edge[MAX+5];
int main()
{
int T;
for(scanf("%d",&T);T--;){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lf%lf",&edge[i].x,&edge[i].y);
}
double d=0;
for(int i=0;i<n-1;i++){
d+=-0.5*(edge[i+1].y+edge[i].y)*(edge[i+1].x-edge[i].x);
}
if(d<0){ ///d<0为顺时针
printf("run\n");
}
else{
printf("fight\n");
}
}
return 0;
}