POJ 2653 Pick_up_stick(计算几何)

本文详细解析了Pick-upsticks问题的算法逻辑与实现细节,通过实例输入输出展示了如何解决这个问题,并提供了关键代码片段及注释,旨在帮助读者深入理解并实践算法过程。

B - Pick-up sticks//问题描述
Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.

Input

Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.

Output

For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.

The picture to the right below illustrates the first case from input.

Sample Input

5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0

Sample Output

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.

Hint

Huge input,scanf is recommended.
#include <iostream>//此题还需要改进-注意浮点型精度的运算。
using namespace std;
const double eps = 1e-8;
int dcmp(double x)//根据需要精度的不同来设定eps的大小。//控制误差的函数
{
	if (fabs(x) < eps)
		return 0;
	if (x < 0)
		return -1;
	return 1;
}
struct line
{
	int order;
	int x1, x2, y1, y2;
	line(){}
	line(int _x1, int _y1,int _x2, int _y2)
	{
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;

	}

};
line ans[1000];//利用叉积来确定连续线段的转向,从而确定是否有没有点跨越线段,若有点跨越线段则相交,否则不相交
int main()
{
	int n;
	int GetDirection(int x, int y, line s);
	bool IsIntersect(line in_stack, line pre_stack);
	while (cin >> n)
	{
		if (n == 0)
			break;
		else
		{
			int num = n;
			for (int i = 0; i < n; i++)
			{
				double x1, y1, x2, y2;
				line temp;
				
				cin >> x1 >> y1 >> x2 >> y2;
				temp = line(x1, y1, x2, y2);
				temp.order = i + 1;
				int temp_i = 0;
				for (int j = 0; j < i; j++)
				{
					if (IsIntersect(ans[j], temp) && ans[j].order>0)
					{
						ans[j].order = -1;
						num--;
					}
				}
			
				ans[i] = temp;;

			}
			cout << "Top sticks:";
			int temp_it=0;
			for (int i = 0; i < n; i++)
			{
				
				if (ans[i].order>0)
				{
					temp_it++;
					cout << ans[i].order;
					if (temp_it < num)
						cout << ",";
				}

			}
			cout << "." << endl;


		}
	}

	system("pause");
	return 0;
}
int GetDirection(int x,int y,line s)
{
	int Vector_s_x = s.x1 - s.x2;
	int Vector_s_y = s.y1 - s.y2;
	int Vector_p_x = x - s.x2;
	int Vector_p_y = y - s.y2;
	int res = Vector_p_x*Vector_s_y - Vector_s_x*Vector_p_y;
	return res;
}
bool IsIntersect(line in_stack, line pre_stack)//即将入栈的元素和栈中的元素一一相比较如果相交则使栈中的元素出栈,比较完毕之后让新元素入栈
{
	int d1 = GetDirection(in_stack.x1, in_stack.y1, pre_stack);
	int d2 = GetDirection(in_stack.x2, in_stack.y2, pre_stack);
	int d3 = GetDirection(pre_stack.x1, pre_stack.y1, in_stack);
	int d4 = GetDirection(pre_stack.x2, pre_stack.y2, in_stack);
	if ((d1*d2 < 0) && (d3*d4 < 0))
		return true;
	else
		return false;//此处还可以拓展相交的算法,是否在线段之上。只是此题不需要故略
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值