lightoj1190Sleepwalking【点在多变形内判断(射线法考虑全面)】


E - 
Sleepwalking
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

All of you know about "Alauddin vai". But what you don't know is, he sometimes walks while sleeping. Yes, some kind of sleep walking.

Now one day he was sleeping in a strange room. But after waking up he doubts that whether he is inside the room or not.

You may assume that the room can be modeled as a polygon whose coordinates are 2D integer points. Alauddin vai can be thought as a 2D integer point. Now you are given the configuration of the room and the position of Alauddin vai (after waking up). You have to decide whether he is still in the room or not. Consider him inside if he is on the boundary of the polygon.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with an integer n (3 ≤ n ≤ 100) denoting the number of vertices of the room. The next line contains 2n integers, where the ith pairxi yi denote the co-ordinate of the ith vertex of the room. The vertices will be given in anticlockwise order. The next line contains an integer q (1 ≤ q ≤ 300) denoting the number of queries. Each of the next q lines contains two integers x y denoting the co-ordinate of Alauddin vai. The absolute value of the given co-ordinates will be less than 10001.

Output

For each case, print the case number in a single line. Then print q lines, containing the answer for the queries as given in input. Print 'Yes' if he is inside the room, print 'No' otherwise.

Sample Input

2

3

0 0 10 0 0 20

2

5 5

10 10

3

0 0 3 3 0 3

1

5 5

Sample Output

Case 1:

Yes

No

Case 2:

No

题意:判断点在多边形内
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
#define inf 1e20
using namespace std; 
struct point{
	double x,y;
}A[105],p;
struct line{
	point a,b; 
}B[2];
int N;
double MIN(double a,double b){
	return a<b?a:b;
}
double MAX(double a,double b){
	return a>b?a:b;
}
double cp(point p1,point p2,point p3){
	return (p3.y-p1.y)*(p2.x-p1.x)-(p3.x-p1.x)*(p2.y-p1.y);
}
bool judge1(point p,line l){//判断点在直线上 
    if(fabs(cp(p,l.a,l.b))<=eps&&MIN(l.a.x,l.b.x)<=p.x&&p.x<=MAX(l.a.x,l.b.x)&&MIN(l.a.y,l.b.y)<=p.y&&p.y<=MAX(l.a.y,l.b.y))
    	return true;
   	return false;
}
bool judgeinter(int a,int b){//判断线段相交       
    if(MIN(B[a].a.x,B[a].b.x)>MAX(B[b].a.x,B[b].b.x)||MIN(B[a].a.y,B[a].b.y)>MAX(B[b].a.y,B[b].b.y)||MIN(B[b].a.x,B[b].b.x)>MAX(B[a].a.x,B[a].b.x)||MIN(B[b].a.y,B[b].b.y)>MAX(B[a].a.y,B[a].b.y))          
    return false;          
    double h,i,j,k;          
    h=(B[a].b.x-B[a].a.x)*(B[b].a.y-B[a].a.y)-(B[a].b.y-B[a].a.y)*(B[b].a.x-B[a].a.x);          
    i=(B[a].b.x-B[a].a.x)*(B[b].b.y-B[a].a.y)-(B[a].b.y-B[a].a.y)*(B[b].b.x-B[a].a.x);          
    j=(B[b].b.x-B[b].a.x)*(B[a].a.y-B[b].a.y)-(B[b].b.y-B[b].a.y)*(B[a].a.x-B[b].a.x);          
    k=(B[b].b.x-B[b].a.x)*(B[a].b.y-B[b].a.y)-(B[b].b.y-B[b].a.y)*(B[a].b.x-B[b].a.x);          
    return h*i<=eps&&j*k<=eps;          
}      
bool judge(){
	B[0].a=p;B[0].b.x=inf;B[0].b.y=p.y;
	int ans=0;
	for(int i=0;i<N;++i){
		B[1].a=A[i];
		B[1].b=A[(i+1)%N];
		if(judge1(p,B[1])) 
			return true;
		if(fabs(B[1].a.y-B[1].b.y)<eps)
			continue; 
		if(judge1(B[1].a,B[0])){
			if(B[1].a.y>B[1].b.y)
				ans++;
   		}
		else if(judge1(B[1].b,B[0])){
			if(B[1].b.y>B[1].a.y)
				ans++; 
		}
		else if(judgeinter(0,1))
			ans++; 
	} 
	if(ans&1)
		return true; 
	return false; 
}
int main()
{
	int t,i,j,k=1,m;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&N);
		for(i=0;i<N;++i){
			scanf("%lf%lf",&A[i].x,&A[i].y);
		}
		printf("Case %d:\n",k++);
		scanf("%d",&m);
		while(m--){
			scanf("%lf%lf",&p.x,&p.y);
			if(judge())
				printf("Yes\n");
			else 
				printf("No\n");
		}
	}
	return 0;
}


Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值