【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
题意
给出一系列点的坐标 每两个点表示一条线 最后判断所走路线是顺时针还是逆时针 逆时针输出fight 顺时针输出run

这里有一个公式大家需要知道–鞋带公式

这里给出一个化简的公式
在这里插入图片描述
https://blog.csdn.net/u012138730/article/details/79814650

去掉上述计算S中的绝对值,让其有正负,判断逆顺:
点序为顺, 面积为负值。
点序为逆,面积为正值。

bool solve(){
    //这里有一个约定,当下标大于n的时候X(n+1)=X1,Y(n+1)=Y1
    for(int i=1;i<n;i++){
        if(i==n) num[i+1].y=num[1].y,num[i+1].x=num[1].x;
        ans+=-0.5*(num[i+1].y+num[i].y)*(num[i+1].x-num[i].x);//由于推导公式得到需要加一个负号
    }
    if(ans<0) return true;//小于0就是顺时针,这个可以通过维基百科加以理解
    return false;
}

https://blog.csdn.net/qq_37602930/article/details/80496498
这样有的博客说是格林公式的特殊情况,emmm,以后再去研究
下面是AC的代码

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int N = 1e6+10;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int MOD=1e9+7;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Abs(x) ((x)>=0?(x):-(x))
int n;
double ans;
struct node{
    double x,y;
}num[100];
bool solve(){
    //这里有一个约定,当下标大于n的时候X(n+1)=X1,Y(n+1)=Y1
    for(int i=1;i<n;i++){
        if(i==n) num[i+1].y=num[1].y,num[i+1].x=num[1].x;
        ans+=-0.5*(num[i+1].y+num[i].y)*(num[i+1].x-num[i].x);//由于推导公式得到需要加一个负号
    }
    if(ans<0) return true;//小于0就是顺时针,这个可以通过维基百科加以理解
    return false;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int t;
    scanf("%d",&t);
    while(t--){
        ans=0.0;
        scanf("%d",&n);
        rep(i,1,n) scanf("%lf%lf",&num[i].x,&num[i].y);
        if(solve()) printf("run\n");
        else printf("fight\n");
    }
    return 0;
 }

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页