CodeForces - 1030B Vasya and Cornfield (几何)

29 篇文章 0 订阅
4 篇文章 0 订阅

题目大意:给出两个整数n,d,代表有一个在第一象限的矩形,坐标是(0,d) (d,0) (n,n-d) (n-d,n),再给出一些点的坐标,求出这些点是否在矩形中。

题解:只需要判断该点是否在上下两条边和左右两条边之间就可以

法①:

        判断一个点是否在两条线段之间夹着,就转化成,判断一个点是否在某条线段的一边上,就可以利用叉乘的方向性,来判断夹角是否超过了180度

只要判断

(\overrightarrow{p_{1}p_{2}}\times \overrightarrow{p_{1}p})*(\overrightarrow{p_{3}p_{4}}\times \overrightarrow{p_{3}p})>=0(向量叉积)

就说明p在边p1p2和边p3p4中间夹着,同理也要判断

(\overrightarrow{p_{2}p_{3}}\times \overrightarrow{p_{2}p})*(\overrightarrow{p_{4}p_{1}}\times \overrightarrow{p_{4}p})>=0

#include <bits/stdc++.h>
#include <cstring>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
struct Point
{
    double x,y;
    Point() {}
    Point(double _x,double _y)
    {
        x = _x;
        y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
//叉积
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
//点积
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
//绕原点旋转角度B(弧度值),后x,y的变化
    void transXY(double B)
    {
        double tx = x,ty = y;
        x = tx*cos(B) - ty*sin(B);
        y = tx*sin(B) + ty*cos(B);
    }
};
Point p1,p2,p3,p4;
double GetCross(Point& p1,Point& p2,Point& p)
{
    return (p2-p1)^(p-p1);
}
bool pd(Point p)
{
    return GetCross(p1,p2,p) * GetCross(p3,p4,p) >= 0 && GetCross(p2,p3,p) * GetCross(p4,p1,p) >= 0;
}
int main()
{
    int n,d;
    cin>>n>>d;
    p1=Point(0,d);
    p2=Point(d,0);
    p3=Point(n,n-d);
    p4=Point(n-d,n);
    int T;
    cin>>T;
    while(T--)
    {
        int x,y;
        cin>>x>>y;
        if(pd(Point(x,y)))
            puts("YES");
        else puts("NO");
    }
    return 0;
}

法②:

只需要满足

d\leq x+y\leq 2n-d

-d\leq x-y\leq d

就是在矩形内

#include <bits/stdc++.h>
#include <cstring>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
    int n,d,T;
    cin>>n>>d;
    cin>>T;
    while(T--)
    {
        int x,y;
        cin>>x>>y;
        if(x+y>=d && x+y<=2*n-d && x-y>=-d && x-y<=d)
            puts("YES");
        else puts("NO");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值