嘟嘟嘟

题面:给一个\(n\)个点的多边形和\(m\)个点,判断每一个点是否在多边形内。

解法:射线法。

就是从这个点引一条射线,如果与多边形有奇数个交点,则在多边形内部。

那么只用枚举每一条边,然后判断这条边与射线有无交点。为了方便,射线为水平的。然后可以用叉积判断三点共线,以及多边形的两个端点纵坐标的大小关系。

但要注意一些特殊情况,比如有一个交点是多边形的顶点,所以为了避免重复统计,需要规定交在每一条边的下断点还是上端点。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 105;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, m; struct Vec
{
int x, y;
db operator * (const Vec& oth)const
{
return x * oth.y - oth.x * y;
}
friend int dot(const Vec& A, const Vec& B)
{
return A.x * B.x + A.y * B.y;
}
};
struct Point
{
int x, y;
Vec operator - (const Point& oth)const
{
return (Vec){x - oth.x, y - oth.y};
}
}A[maxn], P; bool judge()
{
int cnt = 0;
A[n + 1] = A[1];
for(int i = 1; i <= n; ++i)
{
int d = (P - A[i]) * (P - A[i + 1]);
if(!d && dot(A[i] - P, A[i + 1] - P) <= 0) return 1; //点在边上
int d1 = A[i].y - P.y, d2 = A[i + 1].y - P.y;
if(d > 0 && d1 >= 0 && d2 < 0) cnt ^= 1;
if(d < 0 && d1 < 0 && d2 >= 0) cnt ^= 1;
}
return cnt;
} int main()
{
int cnt = 0;
while(scanf("%d", &n) && n)
{
if(++cnt != 1) enter;
printf("Problem %d:\n", cnt);
m = read();
for(int i = 1; i <= n; ++i) A[i].x = read(), A[i].y = read();
for(int i = 1; i <= m; ++i)
{
P.x = read(); P.y = read();
puts(judge() ? "Within" : "Outside");
}
}
return 0;
}

ZOJ1081 Points Within的更多相关文章

  1. ZOJ1081 Points Within 点和多边形的位置关系

    ZOJ1081 给一个点和一个多边形 判断点在多边形内(边上)还是在多边形外 在多边形外的点引一条射线必然穿过多边形的两条边 而在多边形内的点则不一定. 当然凹多边形有特殊情况 但是总能找到对应位置关 ...

  2. ZOJ1081:Points Within——题解

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1081 题目大意:给定一个点数为 n 的多边形,点按照顺序给出,再给出 m ...

  3. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  4. [LeetCode] Max Points on a Line 共线点个数

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  5. LeetCode:Max Points on a Line

    题目链接 Given n points on a 2D plane, find the maximum number of points that lie on the same straight l ...

  6. K closest points

    Find the K closest points to a target point in a 2D plane. class Point { public int x; public int y; ...

  7. 【leetcode】Max Points on a Line

    Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...

  8. Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  9. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

随机推荐

  1. C#PrintDocument打印尺寸调整

    /// <summary> /// 打印的按钮 /// </summary> /// <param name="sender"></par ...

  2. 十六、Condition等待通知

    一.简介 我们可以使用syncronized和wait,notify实现等待通知.而syncronized的高级实现Lock,也可以实现等待通知,需要构造Condition的实例对象. JDK文档:h ...

  3. 03-Tomcat服务器

    一.Java分类 JavaSE Java的标准版,一般用来开发桌面应用程序, 但是在开发桌面应用程序上相对VB,Delphi,VC++并没有什么优势. JavaEE 也就是Java Enterpris ...

  4. Java 的 委托 是什么?

    前言:在学习设计模式时,发现书中有多次提到委托二字,所以经过网上搜索得到结果,并自己写了个小小的例子. 什么是委托? 委托模式是软件设计模式中的一项基本技巧.在委托模式中,有两个对象参与处理同一个请求 ...

  5. ES6学习笔记(一)-变量的解构赋值

    变量的解构赋值种类 解构(Destructuring):ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值. 只有当一个数组成员严格等于(===)undefined,包括空“ ” ,默认值 ...

  6. HTML中的图片

    在一开始时,Web仅有文本,那真的是很无趣.幸运的是,没过多久网页上就能嵌入图片和其他有趣的内容了.虽然还有许多其他类型的多媒体,但是从地位比较低的<img>元素开始是符合逻辑的,它常常被 ...

  7. INNODB与MyISAM两种表存储引擎区别

    mysql数据库分类为INNODB为MyISAM两种表存储引擎了,两种各有优化在不同类型网站可能选择不同,下面小编为各位介绍mysql更改表引擎INNODB为MyISAM技巧. 常见的mysql表引擎 ...

  8. Android深入四大组件(五)Android8.0 根Activity启动过程(后篇)

    前言 在几个月前我写了Android深入四大组件(一)应用程序启动过程(前篇)和Android深入四大组件(一)应用程序启动过程(后篇)这两篇文章,它们都是基于Android 7.0,当我开始阅读An ...

  9. python oop常用术语 继承 多态 封装

    面向对象优点 1.通过封装明确了内外 2.通过继承+多态在语言层面支持了归一化设计 抽象/实现 抽象指对现实世界问题和实体的本质表现,行为和特征建模,建立一个相关的子集,可以用于 绘程序结构,从而实现 ...

  10. JMeter测试WEB性能入门

    一.JMeter介绍 1.Apache JMeter是什么 Apache JMeter 是Apache组织的开放源代码项目,是一个100%纯Java桌面应用,用于压力测试和性能测量.它最初被设计用于W ...