[Swust OJ 567]--老虎在不在笼子里(凸包问题)
题目链接:http://acm.swust.edu.cn/problem/567/
你的任务就是通过GPS给出的老虎的坐标和笼子的坐标来让电脑计算出老虎的位置,是躲在洞里了,还是跑到外面去了.
第一行是一个数据N(0<=N<=12),代表了笼子的坐标X,Y的个数(X,Y都在INT范围内);
第二到N+1行是N个坐标X,Y,依次按从下逆时针的顺序来给出的.
最后一行是老虎的坐标X,Y;
注:只有一组测试数据,输入的都是整数,笼子一定是凸多边形,而且老虎也不会出现在笼子边界上
三角形的面积可以用向量的外积来计算,那么多边形是N个三角形,那么就可以。。。。。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
4
0 0
1 0
1 1
0 1
0.5 0.5
6
1 -1
2 0
1 1
-1 1
-2 0
-1 -1
3 3
|
|
1
2
|
YES
NO
|
//只需要判断加入该点凸包面积,和笼子构成凸包面积是否相等即可 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cmath>
using namespace std;
struct node{
double x, y;
}point[], tiger[], posA, posB, tmp;//posB老虎基准量 double dis(node a, node b){
return pow((a.x - b.y), ) + pow((a.y - b.y), );
} //按点集分布排序,利用向量平行关系判断
bool cmp(node a, node b){
double povit = (a.x - posA.x)*(b.y - posA.y) - (b.x - posA.x)*(a.y - posA.y);
if (povit > || !povit && (dis(a, posA) < dis(b, posA)))
return true;
return false;
} //当前点是否在点集左侧,利用叉乘比较3个点两条线的斜率关系
bool turn_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) > ? true : false;
} int main(){
int i, sign1, sign2, n;
double area1, area2;
cin >> n;
for (i = ; i < n; i++){
cin >> point[i].x >> point[i].y;
tiger[i].x = point[i].x;
tiger[i].y = point[i].y;
}
cin >> tiger[i].x >> tiger[i].y;//老虎坐标
stack<node> Q;
sign1 = ;
posA = posB = point[];
//分别找到笼子,加入老虎坐标 的点集的最左下的点
for (i = ; i < n + ; 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;
}
}
else{
posB = posA;
sign2 = sign1;
if (posB.y == tiger[i].y&&posB.x>tiger[i].x || tiger[i].y < posB.y){
posB = tiger[i];
sign2 = i;
}
}
}
swap(point[], point[sign1]);
swap(tiger[], tiger[sign2]);
sort(point + , point + n, cmp);//利用叉乘按点集离散化,方便筛选构成图凸包有效的点
sort(tiger + , tiger + n + , cmp);
Q.push(tiger[]), Q.push(tiger[]), Q.push(tiger[]); for (i = ; i < n + ; i++){
while (!Q.empty()){
tmp = Q.top();
Q.pop();
if (turn_left(tmp, Q.top(), tiger[i])){
Q.push(tmp);
break;
}
}
Q.push(tiger[i]);
}
//由于给出笼子必为凸包,直接计算面积
area1 = area2 = ;
tmp = point[n - ];
area1 += (posA.x*tmp.y - posA.y*tmp.x) / 2.0;
for (i = n - ; i >= ; i--){
area1 += (tmp.x*point[i].y - tmp.y*point[i].x) / 2.0;
tmp = point[i];
} tmp = Q.top(), Q.pop();
area2 += (posA.x*tmp.y - posA.y*tmp.x) / 2.0;
while (!Q.empty()){
area2 += (tmp.x*Q.top().y - tmp.y*Q.top().x) / 2.0;
tmp = Q.top();
Q.pop();
}
printf("%s\n", area1 == area2 ? "YES" : "NO");
return ;
}
[Swust OJ 567]--老虎在不在笼子里(凸包问题)的更多相关文章
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- OJ提交题目中的语言选项里G++与C++的区别
一.OJ提交题目中的语言选项里G++与C++的区别 http://www.th7.cn/Program/cp/201405/199001.shtml 首先更正一个概念,C++是一门计算机编程语言,G+ ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- [Swust OJ 1026]--Egg pain's hzf
题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf ...
- [Swust OJ 1139]--Coin-row problem
题目链接: http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...
随机推荐
- Java format 简单应用
一.前言 String.format 作为文本处理工具,为我们提供强大而丰富的字符串格式化功能,为了不止步于简单调用 String.format("Hello %s", " ...
- 未能从程序集“System.ServiceModel,xxx”中加载类型“System.ServiceModel.Activation.HttpModule”。
一.平台环境 二.问题描述 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561 ...
- 织梦dedecms|文章页通用标签
当前位置: {dede:field name='position'/}上一页: {dede:prenext get='pre'/}下一页: {dede:prenext get='next'/}收 ...
- 高性能javascript 学习笔记(1)
加载和运行 管理浏览器中的javascript代码是个棘手的问题,因为代码运行阻塞了其他浏览器处理过程,诸如用户绘制,每次遇到<script>标签,页面必须停下来等待代码下载(如果是外部的 ...
- [LeetCode]题解(python):076-Minimum Window Substring
题目来源: https://leetcode.com/problems/minimum-window-substring/ 题意分析: 给定两个字符串S和T.在S中找到最短的一个子字符串使得他包括所有 ...
- KVO 的使用和举例
KVO(key-value Observer),通过命名可以联想到,一个监视着监视着键值配对,让一个对象A来监视另一个对象B中的键值,一旦B中的受监视键所对应的值发生了变化,对象A会进入一个回调函数, ...
- C#共享内存类改进版
原文 C#共享内存类改进版 改进说明及源码实例下载见:http://blog.csdn.net/zzh8845/archive/2008/11/22/3349963.aspx ShareMem.cs ...
- Ants(思维)
Ants Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12893 Accepted: 5637 Description ...
- jQ 操作积累
1.判断radio是否选中:方式一:var val=$('input:radio[name="sex"]:checked').val(); //(val==null 未选中) 方式 ...
- Knockout快速扫盲
1.Knockout.js是什么? Knockout是一款很优秀的js库,它可以快速让你使用一个干净整洁的底层数据模型,即可创建一个富文本且具有良好显示和编辑功能的用户界面.任何时候你的ui需要自 ...