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

大洪水来了【附判断相交的方法和代妈】

Posted by KatrineYang at 2016-07-25 19:59:30 on Problem 1127 and last updated at 2016-07-25 20:03:41
xj()这个函数是核心,负责判断(x1,y1)(x2,y2)和(x3,y3)(x4,y4)是否相交。
如果四个点不醛共线的話,那麽使用三角形面积的正负,即132,324,241,413这四个三角形的有向面积或者醛正,或者醛负(洞又算正,又算负)。但是如果这四个面积都是洞(代表4个点醛部共线),那麽不熊用这个方法,此时使用maxmin<minmax来判断很简单的

#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;

int MAX(int x, int y){
	return x>y ? x : y;
}

int MIN(int x, int y){
	return x<y ? x : y;
}

bool xj(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
	int s1 = x1*y3+x3*y2+x2*y1-x3*y1-x2*y3-x1*y2;
	int s2 = x3*y2+x2*y4+x4*y3-x2*y3-x4*y2-x3*y4;
	int s3 = x2*y4+x4*y1+x1*y2-x4*y2-x1*y4-x2*y1;
	int s4 = x4*y1+x1*y3+x3*y4-x1*y4-x3*y1-x4*y3;
	if(s1 != 0 || s2 != 0 || s3 != 0 || s4 != 0){
		return (s1>=0 && s2>=0 && s3>=0 && s4>=0) || (s1<=0 && s2<=0 && s3<=0 && s4<=0);
	}
	//四点共线
	return MAX(MIN(x1, x2), MIN(x3, x4)) <= MIN(MAX(x1, x2), MAX(x3, x4)) && MAX(MIN(y1, y2), MIN(y3, y4)) <= MIN(MAX(y1, y2), MAX(y3, y4));
}

int main() {
	int N;
	while(1){
		scanf("%d", &N);
		if(N == 0) return N;
		int startX[20], startY[20], endX[20], endY[20];
		for(int i = 1; i <= N; i++){
			scanf("%d%d%d%d", startX+i, startY+i, endX+i, endY+i);
		}
		bool direct[20][20] = {false};
		for(int i = 1; i <= N; i++){
			for(int j = i; j <= N; j++){
				if(i == j) direct[i][j] = true;
				else{
					direct[i][j] = xj(startX[i], startY[i], endX[i], endY[i], startX[j], startY[j], endX[j], endY[j]);
					direct[j][i] = direct[i][j];
				}
			}
		}
		bool conn[20][20] = {false};
		for(int i = 1; i <= N; i++){
			bool state[20] = {false};
			state[i] = true;
			conn[i][i] = true;
			queue<int> bfs;
			bfs.push(i);
			while(!bfs.empty()){
				int idx = bfs.front();
				bfs.pop();
				for(int j = 1; j <= N; j++){
					if(!direct[idx][j] || state[j]) continue;
					state[j] = true;
					conn[i][j] = true;
					bfs.push(j);
				}
			}
		}
		int d1, d2;
		while(1){
			scanf("%d%d", &d1, &d2);
			if(d1 == 0 && d2 == 0) break;
			if(conn[d1][d2]) printf("CONNECTED\n");
			else printf("NOT CONNECTED\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