uva 361 - Cops and Robbers(凸包)

题目中给出了n个cops和m个robbers和q个居民,如果一个居民在某三个cops围成的三角形中就是安全的,否则,如果在某三个robbers围成的三角形中,就是不安全的,不然就是neither。



思路:这个可以转换成凸包来做。判断某个居民是不是在某个凸包内部就行了。

:下面是凸包的求法之一

int getConvexHull (Point* p, int n, Point* ch) {
    sort(p, p + n);
    // for (int i = 0;i < n;++i)
    //     p[i].write(), puts("");
    // puts("");
    int m = 0;
    for (int i = 0; i < n; i++) {
        /* 可共线 */
        //while (m > 1 && dcmp(getCross(ch[m-1]-ch[m-2], p[i]-ch[m-1])) < 0) m--;
        while (m > 1 && dcmp(getCross(ch[m-1]-ch[m-2], p[i]-ch[m-1])) <= 0)
        {
            // ch[m-1].write(), puts("*******");
            m--;
        }
        ch[m++] = p[i];
    }
    int k = m;
    for (int i = n-2; i >= 0; i--) {
        /* 可共线 */
        //while (m > k && dcmp(getCross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) < 0) m--;
        /*不可共线*/
        while (m > k && dcmp(getCross(ch[m-1]-ch[m-2], p[i]-ch[m-1])) <= 0) {
            // ch[m-1].write(),puts("$$$$$$$$$$");
            m--;
        }
        ch[m++] = p[i];
    }
    if (n > 1) m--;
    // for (int i = 0;i < m;++i)
    //     ch[i].write(),puts("^^^^^^^^");
    // puts("");
    return m;
}
点在凸多边形内的判断
int onPolygonal (Point a, Point* p, int n) {
    for (int i = 0; i < n; i++) if (a == p[i]) return 0;
    if (n >= 2) {
        for (int i = 0; i < n; i++) {
            int v = (i + 1) % n;
            if (onSegment(a, p[i], p[v])) return 0;
        }
    }
    if (n <= 2) return 1;

    int sig = dcmp((a - p[0]) * (p[1] - p[0]));
    for (int i = 0; i < n; i++) {
        int v = (i + 1) % n;
        int tsig = dcmp((a - p[i]) * (p[v] - p[i]));
        if (tsig != sig)
            return 1;//在凸多边形外
    }
    return -1;
}
时间: 2024-08-05 10:23:12

uva 361 - Cops and Robbers(凸包)的相关文章

UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)

题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees from thieves, the king or

UVA 11800 Determine the Shape --凸包第一题

题意: 给四个点,判断四边形的形状.可能是正方形,矩形,菱形,平行四边形,梯形或普通四边形. 解法: 开始还在纠结怎么将四个点按序排好,如果直接处理的话,有点麻烦,原来凸包就可搞,直接求个凸包,然后点就自动按逆时针排好了,然后就判断就可以了,判断依据题目下面有,主要是用到点积和叉积,判断垂直用点积,判断平行用叉积. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstd

UVa 10256 (判断两个凸包相离) The Great Divide

题意: 给出n个红点,m个蓝点.问是否存在一条直线使得红点和蓝点分别分布在直线的两侧,这些点不能再直线上. 分析: 求出两种点的凸包,如果两个凸包相离的话,则存在这样一条直线. 判断凸包相离需要判断这两件事情: 任何一个凸包的任何一个顶点不能在另一个凸包的内部或者边界上. 两个凸包的任意两边不能相交. 二者缺一不可,第一条很好理解,但为什么还要判断第二条,因为存在这种情况: 虽然每个凸包的顶点都在另一个凸包的外部,但两个凸包明显是相交的. 1 //#define LOCAL 2 #include

UVA 10652 Board Wrapping(凸包)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32286 [思路] 凸包 根据角度与中心点求出长方形所有点来,然后就可以应用凸包算法了. [代码] #include<cmath> #include<cstdio> #include<algorithm> using namespace std; const double PI = acos(-1.0); double torad(doub

UVA 10256 The Great Divide(凸包划分)

The Great Divide Input: standard input Output: standard output Time Limit: 8 seconds Memory Limit: 32 MB Somewhere in Gaul, there is a little village very like the village where Asterix and Obelix live. Not very long ago they had only one chief Altru

2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Solution

A:Exam Solved. 温暖的签. 1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 1e3 + 10; 6 7 int k; 8 char str1[maxn], str2[maxn]; 9 10 int main() 11 { 12 while(~scanf("%d",&k)) 13 { 14 scanf("%s", str1 + 1); 15

ComWin’ round 11部分题解

https://vjudge.net/contest/325913#overview A.Threehouses 题意:一直二维平面上的$n$个点中,前$e$个点落在小岛周围,并且有$p$条边已经连接,问最少花费使得所有点都可以通过一些边到达小岛,两点之间建边的花费为两点间的欧式距离. 思路:根据$kruskal$求最小生成树的方法将前$e$个点合并起来,再将已有$p$条边的两点合并,之后做一次$kruskal$把所有点合并即可. #include<bits/stdc++.h> using n

【最小矩形面积覆盖:凸包+旋转卡壳】UVA 10173 Smallest Bounding Rectangle

[最小矩形面积覆盖:凸包+旋转卡壳]UVA 10173 Smallest Bounding Rectangle 题目链接:UVA 10173 Smallest Bounding Rectangle 题目大意 给你n个点,求能够覆盖所有点集的最小矩形面积. 笔者的第2道凸包题目,凸包 + 旋转卡壳,实现点集的最小矩形面积覆盖问题 ">=0"写成"<=0"坑了我一下午!QAQ 说一下思路 ①Graham's Scan法构建凸包,时间复杂度O(nlogn) ②

uva 10065 (凸包+求面积)

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=1006 Problem A Useless Tile Packers Input: standard input Output: standard output Yes, as you have apprehended the Useless Tile Pac