算法复习——计算几何基础(zoj1081)
题目:
Statement of the Problem
Several drawing applications allow us to draw polygons and almost all of them allow us to fill them with some color. The task of filling a polygon reduces to knowing which points are inside it, so programmers have to colour only those points.
You're expected to write a program which tells us if a given point lies inside a given polygon described by the coordinates of its vertices. You can assume that if a point is in the border of the polygon, then it is in fact inside the polygon.
Input Format
The input file may contain several instances of the problem. Each instance consists of: (i) one line containing integers N, 0 < N < 100 and M, respectively the number of vertices of the polygon and the number of points to be tested. (ii) N lines, each containing a pair of integers describing the coordinates of the polygon's vertices; (iii) M lines, each containing a pair of integer coordinates of the points which will be tested for "withinness" in the polygon.
You may assume that: the vertices are all distinct; consecutive vertices in the input are adjacent in the polygon; the last vertex is adjacent to the first one; and the resulting polygon is simple, that is, every vertex is incident with exactly two edges and two edges only intersect at their common endpoint. The last instance is followed by a line with a 0 (zero).
Output Format
For the ith instance in the input, you have to write one line in the output with the phrase "Problem i:", followed by several lines, one for each point tested, in the order they appear in the input. Each of these lines should read "Within" or "Outside", depending on the outcome of the test. The output of two consecutive instances should be separated by a blank line.
Sample Input
3 1
0 0
0 5
5 0
10 2
3 2
4 4
3 1
1 2
1 3
2 2
0
Sample Output
Problem 1:
Outside
Problem 2:
Outside
Within
题解:
一道很基础的计算几何题···判断点是否在一个多边形内··方法是过该点做一条平行的射线看与多边形的交点是否为奇数个···开始的时候可以先判断下共线来优化一下···
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int R()
{
int i=,f=;
char c;
for(c=getchar();(c<''||c>'')&&c!='-';c=getchar());
if(c=='-')
{
i=-;
c=getchar();
}
for(;c>=''&&c<='';c=getchar())
f=(f<<)+(f<<)+c-'';
return i*f;
}
const int N=;
struct point
{
int x;
int y;
}q[N],p;
int n,m,T; inline point operator - (point a,point b)
{
point t;
t.x=a.x-b.x;
t.y=a.y-b.y;
return t;
} inline int operator * (point a,point b)
{
return a.x*b.y-a.y*b.x;
} inline int dot(point a,point b)
{
return a.x*b.x+a.y*b.y;
} inline void pre()
{
q[n]=q[];
int temp;
for(int i=;i<n;i++)
temp+=q[i]*q[i+];
if(temp<=)
{
reverse(q,q+n);
q[n]=q[];
}
} inline bool check(point a,point b,point c)
{
point temp1=a-c;
point temp2=b-c;
if(temp1*temp2!=) return false;
else
{
if(dot(temp1,temp2)<=) return true;
else return false;
}
} inline bool jud()
{
int cnt=;
for(int i=;i<n;i++)
{
if(check(q[i],q[i+],p)) return true;
int t1=q[i].y-p.y;
int t2=q[i+].y-p.y;
point temp1=q[i]-p;
point temp2=q[i+]-p;
if((temp1*temp2>=&&t1>=&&t2<)||(temp1*temp2<=&&t1<&&t2>=))
cnt++;
}
if(cnt%!=) return true;
else return false;
}
int main()
{
//freopen("a.in","r",stdin);
while(true)
{
n=R();
if(n==) break;
T++;
if(T!=) cout<<endl;
cout<<"Problem "<<T<<":"<<endl;
m=R();
for(int i=;i<n;i++)
q[i].x=R(),q[i].y=R();
pre();
for(int i=;i<=m;i++)
{
p.x=R();
p.y=R();
if(jud()) cout<<"Within"<<endl;
else cout<<"Outside"<<endl;
}
}
return ;
}
算法复习——计算几何基础(zoj1081)的更多相关文章
- variable-precision SWAR算法:计算Hamming Weight
variable-precision SWAR算法:计算Hamming Weight 转自我的Github 最近看书看到了一个计算Hamming Weight的算法,觉得挺巧妙的,纪录一下. Hamm ...
- 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...
- nyis oj 68 三点顺序 (计算几何基础)
三点顺序 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 如今给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,如今让你推断A,B,C是顺时针给出的还是逆 ...
- C#冒泡算法复习
C#冒泡算法复习 冒泡算法的意思:每一趟找到一个最小或最大的数放到最后面,比较总数的n-1次(因为比较是2个双双比较的) 第一层循环表示进行比较的次数,总共要比较(数的)-1次 (因为比较是2个双双比 ...
- Python C3 算法 手动计算顺序
Python C3 算法 手动计算顺序 手动计算类继承C3算法原则: 以所求类的直接子类的数目分成相应部分 按照从左往右的顺序依次写出继承关系 继承关系第一个第一位,在所有后面关系都是第一个出现的 ...
- Java实现 蓝桥杯 算法提高 计算超阶乘(暴力)
试题 算法提高 计算超阶乘 问题描述 计算1*(1+k)(1+2k)(1+3k)-(1+n*k-k)的末尾有多少个0,最后一位非0位是多少. 输入格式 输入的第一行包含两个整数n, k. 输出格式 输 ...
- Java实现 蓝桥杯VIP 算法提高 计算时间
算法提高 计算时间 时间限制:1.0s 内存限制:512.0MB 问题描述 给定一个t,将t秒转化为HH:MM:SS的形式,表示HH小时MM分钟SS秒.HH,MM,SS均是两位数,如果小于10用0补到 ...
- Java实现 蓝桥杯 算法提高 计算行列式
试题 算法提高 计算行列式 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 //据说很多人的题目会有一大堆废话,本傻×就不在这里废话了. 给定一个N×N的矩阵A,求|A|. 输入格式 ...
- 计算几何基础算法几何C++实现
This file is implementation of Common Common Computational Geometry Algorithms.Please please pay att ...
随机推荐
- 中国区 Azure 和全球版 Azure:功能对比
由世纪互联运营的 Microsoft Azure(文中简称为中国区 Azure)是在中国大陆独立运营的公有云平台,与全球其他地区由微软运营的 Azure (文中简称全球版 Azure)服务在物理上和逻 ...
- 从零开发分布式数据库中间件 二、构建MyBatis的读写分离数据库中间件
在上一节 从零开发分布式数据库中间件 一.读写分离的数据库中间件 中,我们讲了如何通过ThreadLocal来指定每次访问的数据源,并通过jdbc的连接方式来切换数据源,那么这一节我们使用我们常用的数 ...
- 第14周翻译:SQL Server的阶梯安全级别2
SQL Server的阶梯安全级别2:身份验证 源自:http://www.sqlservercentral.com/articles/Stairway+Series/109975/ 作者:Don K ...
- MySQL 导出一句话
听说是很老的东西了,学习的时候发现还是很好用的,故学习转载过来,留备学习. mysql 导出一句话 方法1:网上流行的方法 流程:(1)建表--->(2)插入数据--->(3)select ...
- POJ 2955 Brackets (区间DP,常规)
题意: 给出一个字符串,其中仅仅含 “ ( ) [ ] ” 这4钟符号,问最长的合法符号序列有多长?(必须合法的配对,不能混搭) 思路: 区间DP的常规问题吧,还是枚举区间[i->j]再枚举其中 ...
- 安卓统一推送联盟融云成唯一IM云服务企业
10月16日,安卓统一推送联盟在北京正式成立,来自中国信息通信研究院,华为.小米.OPPO等手机厂商,BAT等互联网巨头公司等75家机构及企业代表参加了联盟成立大会,融云也受邀参会并成为首批成员单位中 ...
- spark 的RDD各种转换和动作
今天先把spark的各种基本转换和动作总结下,以后有时间把各种用法放上去. 1 RDD基本转换操作 map.flagMap.distinct coalesce.repartition coale ...
- Dockerfile优化建议
1. 减少镜像层 一次RUN指令形成新的一层,尽量Shell命令都写在一行,减少镜像层. 2. 优化镜像大小:清理无用数据 一次RUN形成新的一层,如果没有在同一层删除,无论文件是否最后删除,都会带到 ...
- Delphi与JAVA互加解密AES算法
搞了半天终于把这个对应的参数搞上了,话不多说,先干上代码: package com.bss.util; import java.io.UnsupportedEncodingException; imp ...
- jquery 获得某一组name的id并合并
var attachmentids = $("input[name='attachmentid']").map(function(){return $(this).val()}). ...