算法复习——计算几何基础(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 ...
随机推荐
- C++拾遗(四)——顺序容器
之前一篇博文(<初窥标准库>)简单了解了一种最常用的顺序容器:vector类型.本文将对该文内容进行进一步的学习和完善,继续讨论标准库提供的顺序容器类型.所谓顺序容器,即将单一类型的元素聚 ...
- (一)SpringMVC之警告: No mapping found for HTTP request with URI
这个警告往往是因为url路径不正确. 所以从三个地方下手: 1.springmvc-config.xml中的配置handle,看看是不是因为handle没有配置导致的. 2.如果是使用注解的方式的话, ...
- Python学习日志9月16日
刚才我差点睡着了,差资料的时候太费神,有些累. 今天早晨学习了<head first HTML and CSS>,今天把昨天没看了的关于字体和颜色的一章节看完了,真长.我详细的做了笔记,并 ...
- java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'
java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName' ...
- Docker和K8S
干货满满!10分钟看懂Docker和K8S [摘自:https://my.oschina.net/jamesview/blog/2994112] 本文来源微信号:鲜枣课堂 2010年,几个搞IT的 ...
- linux环境nginx的安装与使用
因为公司需要需要安装一系列环境,新手上路第一次配的时候什么也不懂在网上找了半天,觉得这篇不错,我在这里顺便记录一下.(原文:https://www.cnblogs.com/wyd168/p/66365 ...
- shell脚本,如何用shell打印金字塔
- CentOS6、7安装MySQL5.7全教程
CentOS6.7安装MySQL5.7全教程 做开发总得用到数据吧,Linux作为服务器,总得有一个数据库来存储测试用的数据,所以呢,这里附上CentOS6.7安装MySQL5.7的教程喔~ 用到的工 ...
- 在已编译安装nginx上动态添加模块
一.添加nginx模块 找到安装nginx的源码根目录,如果没有的话下载新的源码 wget http://nginx.org/download/nginx-1.8.1.tar.gz 查看ngixn版本 ...
- Cassandra 数据库安装部署
安装版本 cassandra-3.11.4 系统版本 more /etc/redhat-release CentOS Linux release 7.6.1810 (Core) 准备工作 Cassan ...