[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 ...
随机推荐
- BZOJ 3156: 防御准备( dp + 斜率优化 )
dp(i)表示处理完[i,n]且i是放守卫塔的最小费用. dp(i) = min{dp(j) + (j-i)(j-i-1)/2}+costi(i<j≤N) 然后斜率优化 ------------ ...
- BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )
直接floyd.. ---------------------------------------------------------------------------- #include<c ...
- php 通过referer防盗链(以图片为例)
1.在网页里访问站外图片时,服务器如何知道是在站外引用的呢? (1)对比本服务器请求与跨服务器请求 图一——本服务器请求 图二——显示盗链的referer信息 通过对比也就知道referer显示的是引 ...
- 微信上传素材返回 '{"errcode":41005,"errmsg":"media data missing"}',php5.6返回
问题描述: php5.5已经把通过@加文件路径上传文件的方式给放入到Deprecated中了.php5.6默认是不支持这种方式了 解决办法curl处理 function curl_post($url, ...
- 用PyRestful快速构建Tornado下REST APIs 的支持
一.安装PyRestful库 $ pip install pyrestful 二.使用案例 (一)books_service.py # -*- coding: utf-8 -*- import tor ...
- python安装集成包
anaconda, 包含各种科学运算包以及astropy.装完它一劳永逸. https://www.continuum.io/downloads
- python自学笔记(十一)关于函数及书写格式
1.函数是抽象的第一步 1.1 有关高压锅 1.2 函数是抽象出来的结构,是总结,是方法 1.3 多用函数 2.如何定义函数 2.1 def是关键词, ...
- Sql:查看数据库表和表结构的语句
T-sql 显示表结构和字段信息的sql语句: exec sp_help tablename; ~~使用存储过程 sp_help 显示数据库包含哪些表的sql语句: use yourDBname;se ...
- C++标准程序库的输入输出流(I/O Stream)复制文件(4种方法)
使用C++标准程序库的输入输出流(I/O Stream)复制文件,存在许多的方法, 方法一:逐个字符复制#include < fstream > std::ifstream ...
- char*与char[]
char *s1="hello"; // 指向常量区 char s2[]="hello"; // 指向数组的内存空间 char *s1 的s1是指针,指 ...