NYOJ 78 圈水池( 凸包入门)

2024-08-27 02:58
文章标签 nyoj 78 凸包 水池 入门

本文主要是介绍NYOJ 78 圈水池( 凸包入门),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

链接:click here

题目:有一个牧场,牧场上有很多个供水装置,现在牧场的主人想要用篱笆把这些供水装置圈起来,以防止不是自己的牲畜来喝水,各个水池都标有各自的坐标,现在要你写一个程序利用最短的篱笆将这些供水装置圈起来!(篱笆足够多,并且长度可变)

输出各个篱笆经过各个供水装置的坐标点,并且按照x轴坐标值从小到大输出,如果x轴坐标值相同,再安照y轴坐标值从小到大输出

样例输入
1
4
0 0
1 1
2 3
3 0
样例输出
0 0
2 3
3 0

凸包Graham模板:详细讲解:click here

                                          

第一步:选取x轴坐标最小的点,如果存在多个选y轴坐标最小的点.
第二步:排序.从下到上扫描有其它点扫描的结果顺序,p0, p4, p5, p7, p9,p6, p8, p2,p3, p1.
数学公式向量的外积模公式:|c|(是模不是绝对值符号)= |a|*|b|*sin<a, b> = a.x * b.y – b.x * a.y
|c|> 0:向量a在向量b的顺时针方向,因为此例原点坐标取x轴坐标最小的点,所以,向量a在向量b的下方.(p0-p4线段在p0-p5的下方).|c|= 0:共线时,按照与原点距离由小到大的顺序.
第三步:按照第二步得到的顶点顺序进行graham扫描.按照本例选取的原点方式,当扫描到左拐(p0,p4, p5)时保留(p4),右拐(p9,p6, p8)时删除顶点(p6),结果就是凸包的顶点集.(绿线扫描的顺序,红线为左拐点).共线时,删除距离较小的点.
注意:右拐删除结点时,必须回溯与前面保留的点重新比较,因为删除一个点后先前拐点性质会发生变化

参考自博客:click here

代码:

#include <stdio.h>  //凸包Graham模板
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <math.h>
#include <deque>
#include <algorithm>
#include <iostream>using namespace std;
const double eps=1e-6;
const int maxn=105;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);struct point
{int x,y;
};
point rec1[maxn],rec2[maxn];
int stack[maxn],top;int cross(point p0,point p1,point p2) //计算叉积  p0p1 X p0p2 
{return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}double dis(point p1,point p2)   //计算 p1p2的 距离   /
{return sqrt((double) (p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}bool cmp1(point p1,point p2)//极角排序函数 , 角度相同则距离小的在前面 
{int trap=cross(rec1[0],p1,p2);if(trap>0) return true;else if (trap==0&&dis(rec1[0],p1)<dis(rec1[0],p2)) return true;else return false;
}
bool cmp2(const point &p1,const point &p2)
{return p1.x==p2.x?p1.y<p2.y:p1.x<p2.x;
}
void input(int n)   //输入,并把 最左下方的点放在 list[0] 并且进行极角排序 
{int i,j,k,l;point p0;scanf("%d%d",&rec1[0].x,&rec1[0].y);p0.x=rec1[0].x;p0.y=rec1[0].y;k=0;for(i=1; i<n; i++){scanf("%d%d",&rec1[i].x,&rec1[i].y);if((p0.y>rec1[i].y)||((p0.y==rec1[i].y)&&(p0.x>rec1[i].x))){p0.x=rec1[i].x;p0.y=rec1[i].y;k=i;}}rec1[k]=rec1[0];rec1[0]=p0;sort(rec1+1,rec1+n,cmp1);
}void graham(int n)
{int i;if(n==1) {top=0;stack[0]=0;}if(n==2) {top=1;stack[0]=0;stack[1]=1;}if(n>2){stack[0]=0,stack[1]=1;top=1;for(i=2; i<n; i++){while(top>0&&cross(rec1[stack[top-1]],rec1[stack[top]],rec1[i])<=0) top--;top++;stack[top]=i;}}
}int main()
{int i,j,t,m,n;scanf("%d",&t);while(t--){scanf("%d",&m);input(m);graham(m);double res=0;for(i=0; i<=top; i++){rec2[i].x=rec1[stack[i]].x;rec2[i].y=rec1[stack[i]].y;}sort(rec2,rec2+top+1,cmp2);for(i=0; i<=top; i++)printf("%d %d\n",rec2[i].x,rec2[i].y);}return 0;
}


这篇关于NYOJ 78 圈水池( 凸包入门)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110437

相关文章

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

数论入门整理(updating)

一、gcd lcm 基础中的基础,一般用来处理计算第一步什么的,分数化简之类。 LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } <pre name="code" class="cpp">LL lcm(LL a, LL b){LL c = gcd(a, b);return a / c * b;} 例题:

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

Java 创建图形用户界面(GUI)入门指南(Swing库 JFrame 类)概述

概述 基本概念 Java Swing 的架构 Java Swing 是一个为 Java 设计的 GUI 工具包,是 JAVA 基础类的一部分,基于 Java AWT 构建,提供了一系列轻量级、可定制的图形用户界面(GUI)组件。 与 AWT 相比,Swing 提供了许多比 AWT 更好的屏幕显示元素,更加灵活和可定制,具有更好的跨平台性能。 组件和容器 Java Swing 提供了许多

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

poj 2187 凸包or旋转qia壳法

题意: 给n(50000)个点,求这些点与点之间距离最大的距离。 解析: 先求凸包然后暴力。 或者旋转卡壳大法。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <s

poj 2104 and hdu 2665 划分树模板入门题

题意: 给一个数组n(1e5)个数,给一个范围(fr, to, k),求这个范围中第k大的数。 解析: 划分树入门。 bing神的模板。 坑爹的地方是把-l 看成了-1........ 一直re。 代码: poj 2104: #include <iostream>#include <cstdio>#include <cstdlib>#include <al

MySQL-CRUD入门1

文章目录 认识配置文件client节点mysql节点mysqld节点 数据的添加(Create)添加一行数据添加多行数据两种添加数据的效率对比 数据的查询(Retrieve)全列查询指定列查询查询中带有表达式关于字面量关于as重命名 临时表引入distinct去重order by 排序关于NULL 认识配置文件 在我们的MySQL服务安装好了之后, 会有一个配置文件, 也就

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

HDU 1392 HDU 1348 凸包

求凸包的周长,  注意n=1 , 2时特殊情况 int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}frien