算法复习——计算几何基础(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 ...
随机推荐
- Spark中Java函数的使用方法笔记
1: map 函数map是对RDD中的每个元素都执行一个指定的函数来产生一个新的RDD. 任何原RDD中的元素在新RDD中都有且只有一个元素与之对应. 2: mapPartitions函数</p ...
- Android(java)学习笔记151:Android中操作JSON数据(Json和Jsonarray)
1.Json 和 Xml JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...
- WPF知识点全攻略05- XAML内容控件
此处简单列举出布局控件外,其他常用的控件: Window:WPF窗口 UserControl:用户控件 Page:页 Frame:用来浏览Page页 Border:嵌套控件,提供边框和背景. Butt ...
- 新版raspbian系统的固定IP配置和启用root账户的ssh登录功能的方法
1. 2016新版raspbian系统的固定IP配置: 自2016年2月份新版raspbian系统发布以后,树莓派的固定IP配置方法就与之前不一样了. 之前在raspbian系统中编辑/etc/net ...
- javascript单元测试框架mochajs详解(转载)
章节目录 关于单元测试的想法 mocha单元测试框架简介 安装mocha 一个简单的例子 mocha支持的断言模块 同步代码测试 异步代码测试 promise代码测试 不建议使用箭头函数 钩子函数 钩 ...
- vs 2017 boost 安装目录 非安装
linuxg++ -Wall -std=c++11 boost_socks5.cpp -o boost_socks5 -lboost_system -lboost_thread -lpthread m ...
- tomcat中如何禁止和允许主机或地址访问
1.tomcat中如何禁止和允许列目录下的文件 在{tomcat_home}/conf/web.xml中,把listings参数设置成false即可,如下: <servlet>...< ...
- 寄存器变量 extern 外部变量 外部函数
寄存器变量 这个可以不理睬 register 关键字定义的变量直接放在寄存器当中 寄存器是放在CPU内部的存储单元,它的速度比内存快的多,所以当程序中有10000多次调用同一个变量的时候声明成寄存器变 ...
- ios之UIProgressView
UIProgressView和UIActivityIndicator有些类似 但是不同之处在于, UIProgressView能够更加精确的反应进度 UIActivityIndicator则只能表 ...
- 新建Maven工程,pom.xml报错web.xml is missing and <failOnMissingWebXml> is set to true
错误原因: 项目中没有web.xml 解决办法: 在项目中添加web.xml 在pom.xml中添加下面的插件 <build> <plugins> <plugin> ...