UVA 10078 The Art Gallery【输入点是否全部在凸包上】

题目大意:抽离模型为判定输入点是否全部在凸包上,是输出“No”,否输出“Yes”。
解题策略:求出凸包,直接判定“栈”中顶点数top与nodeNum的关系即可(一开始以为要用到判定点是否在多边形内这个算法,后来发现不需要)


/*
   UVA 10078 The Art Gallery
   AC by J_Dark
   ON 2013/5/6 16:45
   Time 0.012s
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
/
struct point{
	double x, y;
	point(double a, double b){
		x = a;
		y = b;
	}
	double Distance(point t){
		return sqrt((x-t.x)*(x-t.x) + (y-t.y)*(y-t.y));
	}
};
vector<point> p;
vector<int> CH; //存放凸包顶点序号   模拟栈
int testCase, top, cc=0, nodeNum;
/
void Input(){
	p.clear();
	CH.clear();
	CH.resize(nodeNum+5);
	double xx, yy;
	for(int i=0; i<nodeNum; i++){
		cin >> xx >> yy;
		p.push_back(point(xx, yy));
	}
}

bool cmp(point a, point b){
	if(a.y == b.y)  return a.x < b.x;
	return a.y < b.y;
}

bool turnRight(point px1, point px2, point pp){
    const double eps = 1e-20;
	if((px2.x - px1.x)*(pp.y - px2.y) - (pp.x - px2.x)*(px2.y - px1.y) <= eps) return true;
	return false;
}
void ComputeCH(){
	sort(p.begin(), p.end(), cmp);
	CH[0] = 0;
	CH[1] = 1;

	top = 1;
	//从起点0到到排序最后点作凸包右链  过程1
	for(int i=2; i<nodeNum; i++){
		while( top && turnRight(p[CH[top-1]], p[CH[top]], p[i]) )
		{
		   top--;
		}
		CH[++top] = i;
	}

	int len = top;
	//从排序最高点到到起点0fab反向作凸包右链  过程2
	CH[++top] = nodeNum-2;
	for(int i=nodeNum-3; i>=0; i--){
		//top!=len, 不考虑已在过程1生成凸包上的点
		while( top!=len && turnRight(p[CH[top-1]], p[CH[top]], p[i]) )
		{
		   top--;
		}
		CH[++top] = i;
	}
}

void Output(){
	if(top == nodeNum)  cout << "No" << endl;  //输入点全在凸包上
	else  cout << "Yes" << endl;
}

/
int main(){
	while(cin >> nodeNum && nodeNum)
	{
		Input();
		ComputeCH(); //计算凸包
		Output();  //判断并输出
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值