【poj.1127】Jack Straws(计算几何)

Jack Straws

Description

In the game of Jack Straws, a number of plastic or wooden “straws” are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are connected by a path of touching straws. You will be given a list of the endpoints for some straws (as if they were dumped on a large piece of graph paper) and then will be asked if various pairs of straws are connected. Note that touching is connecting, but also two straws can be connected indirectly via other connected straws.
Input

Input consist multiple case,each case consists of multiple lines. The first line will be an integer n (1 < n < 13) giving the number of straws on the table. Each of the next n lines contain 4 positive integers,x1,y1,x2 and y2, giving the coordinates, (x1,y1),(x2,y2) of the endpoints of a single straw. All coordinates will be less than 100. (Note that the straws will be of varying lengths.) The first straw entered will be known as straw #1, the second as straw #2, and so on. The remaining lines of the current case(except for the final line) will each contain two positive integers, a and b, both between 1 and n, inclusive. You are to determine if straw a can be connected to straw b. When a = 0 = b, the current case is terminated.

When n=0,the input is terminated.

There will be no illegal input and there are no zero-length straws.
Output

You should generate a line of output for each line containing a pair a and b, except the final line where a = 0 = b. The line should say simply “CONNECTED”, if straw a is connected to straw b, or “NOT CONNECTED”, if straw a is not connected to straw b. For our purposes, a straw is considered connected to itself.

输入样例

7
1 6 3 3 
4 6 4 9 
4 5 6 7 
1 4 3 5 
3 5 5 5 
5 2 6 3 
5 4 7 2 
1 4 
1 6 
3 3 
6 7 
2 3 
1 3 
0 0

2
0 2 0 0
0 0 0 1
1 1
2 2
1 2
0 0

0

输出样例

CONNECTED 
NOT CONNECTED 
CONNECTED 
CONNECTED 
NOT CONNECTED 
CONNECTED
CONNECTED
CONNECTED
CONNECTED

解题思路

叉积

AC代码

#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int n,f[105][105];
struct node
{
	int x,y;
}a[5],b[15],c[15];
int cj(node x,node y,node z)
{
	return (x.x-z.x)*(y.y-z.y)-(x.y-z.y)*(y.x-z.x);
}
bool check(node x,node y,node z)
{
	if(z.x>=min(x.x,y.x)&&z.x<=max(x.x,y.x)&&z.y>=min(x.y,y.y)&&z.y<=max(x.y,y.y))return 1;
	return 0;
}
int main()
{
	scanf("%d",&n);
	while(n)
	{
		memset(f,0,sizeof(f));
		for(int i=1;i<=n;i++)
			scanf("%d%d%d%d",&b[i].x,&b[i].y,&c[i].x,&c[i].y);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			{
				a[1]=(node){b[i].x,b[i].y};
				a[2]=(node){c[i].x,c[i].y};
				a[3]=(node){b[j].x,b[j].y};
				a[4]=(node){c[j].x,c[j].y};
				int ok=0;
				if(cj(a[3],a[4],a[1])*cj(a[3],a[4],a[2])<0&&cj(a[1],a[2],a[3])*cj(a[1],a[2],a[4])<0)ok=1;
				if(!cj(a[1],a[2],a[3])&&check(a[1],a[2],a[3]))ok=1;
				if(!cj(a[1],a[2],a[4])&&check(a[1],a[2],a[4]))ok=1;
				if(!cj(a[3],a[4],a[1])&&check(a[3],a[4],a[1]))ok=1;
				if(!cj(a[3],a[4],a[2])&&check(a[3],a[4],a[2]))ok=1;
				if(ok)f[i][j]=1;
			}
		for(int k=1;k<=n;k++)
			for(int i=1;i<=n;i++)
				for(int j=1;j<=n;j++)
					f[i][j]=f[i][j]||(f[i][k]&&f[k][j]);
		int x,y;
		scanf("%d%d",&x,&y);
		while(x&&y)
		{
			if(f[x][y])printf("CONNECTED\n");
			else printf("NOT CONNECTED\n");
			scanf("%d%d", &x, &y);
		}
		scanf("%d",&n);
	}
	return 0;
} 

谢谢

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值