您的位置:首页 > 其它

POJ 2653 Pick_up_stick(计算几何)

2015-06-18 16:27 519 查看

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;//此处还可以拓展相交的算法,是否在线段之上。只是此题不需要故略
}


Submit

Status
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: