算法复习——计算几何基础(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 ...
随机推荐
- apache下设置域名多站点访问及禁止apache访问80端口
apache下设置域名多站点访问 当前系统:macOS High Sierra 域名访问配置指定端口后,不同域名只能配置不同的端口 apache配置目录: sudo vim /etc/apache2/ ...
- [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法 --转
初用Yii的srbac模块.出现 Cannot read property ‘msie’ of undefined 错误.上网查询,找到如下的文章.使用文末的打补丁的方法,成功搞定.感谢. ===== ...
- MySQL报错竞技赛
以下报错,我几乎没出过几个. ERROR 2 系统找不到文件: mysql-5.6.1X默认的配置文件是在C:\Program Files\MySQL\MySQL Server 5.6\my-defa ...
- (转)SpringMVC学习(一)——SpringMVC介绍与入门
http://blog.csdn.net/yerenyuan_pku/article/details/72231272 SpringMVC介绍 SpringMVC是什么? SpringMVC和Stru ...
- 线段树成段更新模板POJ3468 zkw以及lazy思想
别人树状数组跑几百毫秒 我跑 2500多 #include<cstdio> #include<map> //#include<bits/stdc++.h> #inc ...
- 数据倾斜是多么痛?spark作业调优秘籍
目录视图 摘要视图 订阅 [观点]物联网与大数据将助推工业应用的崛起,你认同么? CSDN日报20170703——<从高考到程序员——我一直在寻找答案> [直播]探究L ...
- 阿里云apt-get安装包时Err:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main amd64 git amd64 1:2.7.4-0ubuntu1.2 404 Not Found
新部署的云服务器出现如下错误: root@iZj6cbjalvhsw0fhndmm5xZ:~# apt-get install git Reading package lists... Done Bu ...
- OpenRead方法打开文件并读取
实现效果: 知识运用: File类的OpenRead方法 //实现打开现有文件以进行读取 public static FileStream OpenRead(string path) FileStre ...
- Asp.Net Core 入门(十)—— 模型绑定和验证
模型绑定时将Http请求中的数据映射到控制器操作方法上对应的参数,操作方法中的参数可以是简单类型,如整形,字符串等,也可以是复杂类型,如Product,Order等. Asp.Net Core MVC ...
- 利用python进行数据分析1_numpy的基本操作,建模基础
import numpy as np # 生成指定维度的随机多维数据 data=np.random.rand(2,3) print(data) print(type(data)) 结果: [[0.11 ...