Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我都WA了N回了,有哪位过了的帮我看一下啊, 谢谢啦!!

Posted by 77604644 at 2007-04-29 20:58:12 on Problem 1410
//注:在矩形内部也算相交的,与矩形边重叠也算相交!!!
#include <cstdio>
#include <cmath>
int main()
{
	int n, x1, y1, x2, y2, a1, b1, a2, b2;
	scanf("%d", &n);
	for ( int i = 0; i < n; i++ )
	{
		scanf("%d%d%d%d%d%d%d%d", 
			&x1, &y1, &x2, &y2, &a1, &b1, &a2, &b2);
		//判断线段的两个端点是否在矩形内,在就必然相交
		if ( ( (x1-a1)*(x1-a2) <= 0 && (y1-b1)*(y1-b2) <= 0 ) || ( (x2-a1)*(x2-a2) <= 0 && (y2-b1)*(y2-b2) <= 0 ) )
		{
			printf("T\n");
			continue;
		}
		//判断线段的所在的包围矩形 和 矩形之间是否相交,不相交就肯定线段与矩形不相交
		//判断两个矩形相交的算法:左边矩形的lefttop到右边矩形lefttop的水平、垂直距离是否大于第一个矩形的宽、高
		int lefttop1X = x1 > x2 ? x2 : x1;
		int lefttop1Y = y1 > y2 ? y1 : y2;
		int lefttop2X = a1 > a2 ? a2 : a1;
		int lefttop2Y = b1 > b2 ? b1 : b2;
		int width, height;
		if ( lefttop1X > lefttop2X )
		{
			int temp = lefttop1X;
			lefttop1X = lefttop2X;
			lefttop2X = temp;

			temp = lefttop1Y;
			lefttop1Y = lefttop2Y;
			lefttop2Y = temp;

			width = abs ( a1 - a2 );
			height = abs ( b1 - b2 );
		}
		else
		{
			width = abs ( x1 - x2 );
			height = abs ( y1 - y2 );
		}
		if ( abs( lefttop1X - lefttop2X ) > width || abs( lefttop1Y - lefttop2Y ) > height )
		{
			printf("F\n");
			continue;
		}
		//判断矩形的四个顶点是否在线段的两侧,在两侧则相交否则就不相交
		int result1 = (y2-y1)*(a1-x1) - (x2-x1)*(b1-y1);
		int result2 = (y2-y1)*(a1-x1) - (x2-x1)*(b2-y1);
		int result3 = (y2-y1)*(a2-x1) - (x2-x1)*(b1-y1);
		int result4 = (y2-y1)*(a2-x1) - (x2-x1)*(b2-y1);
		if ( result1 > 0 && result2 > 0 && result3 > 0 && result4 > 0 )
		{
			printf("F\n");
		}
		else if ( result1 < 0 && result2 < 0 && result3 < 0 && result4 < 0)
		{
			printf("F\n");
		}
		else
		{
			printf("T\n");
		}
	}
	
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator