POJ 1127 Jack Straws


Jack Straws
Time Limit: 1000MS
 
Memory Limit: 10000K
Total Submissions: 1824
 
Accepted: 817
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.
Sample Input
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

Sample Output
CONNECTED 
NOT CONNECTED 
CONNECTED 
CONNECTED 
NOT CONNECTED 
CONNECTED
CONNECTED
CONNECTED
CONNECTED

Source
East Central North America 1994
 
/* http://acm.pku.edu.cn/JudgeOnline/problem?id=1127기하학적 +를 계산하고 집합을 검사합니다. 처음에는 동적 계획인 줄 착각하고 위로 검색할 수 있는 것을 무시했습니다. 한*/#include#include#define MAX_N 13 #define MAXV(x, y) ((x) >= (y) ? (x) : (y)) #define MINV(x, y) ((x) <= (y) ? (x) : (y)) using namespace std; int num; struct node { int x1, y1, x2, y2; }data[MAX_N + 1]; int set[MAX_N + 1]; int rank[MAX_N + 1];//세 번째 점이 1, 2점으로 구성된 선이 짧은 방향의 어느 방향인지 판단합니다. -1: 시계 반대 방향, 1: 시계 방향 int direct(int x1, int y1, int x2, int x3, int y3){return(x3-x1)*(y2-y1)-(x2-x1)*(y3-y1);}//세 번째 점이 1, 2점으로 구성된 선단상bool onLine(int x1, int y1, int x2, int y2, int x3, int y3) {/cout'MAXV(x1, x2)<>'MINV(y1, y2)<>'MAXV(y1, y2);return(MINV(x1, x2)<=x3 & x3<=MAXV(x1, x2) & MINV(y1, y2)<=y3 & & y3 <= MAXV(y1, y2);}//판단점 1, 2로 구성된 선이 점 3, 4로 구성된 선이 서로 교차하는지 여부boolintersect(intx1, inty1, intx2, intx2, intx3, intx4, inty4) {intd1=direct(x3, y3, x4, y4, x1, y1), intd2=direct(x3, y3, x4, y4, y4, y2), intd3=direct(x1, y1, y1, x2, y3), intd4=direct(x1),, y1, x2, y2, x4, y4);if(((((d1>0 & d2<0)|(d1<0 & d2>0)&(d3>0 & d4<0)|(d3>0 & d4<0)|(d3<0 & d4>0))) return true; if(d1 == 0 && onLine(x3, y3, x4, y4, x1, y1)) return true; else if(d2 == 0 && onLine(x3, y3, x4, y4, x2, y2)) return true; else if(d3 == 0 && onLine(x1, y1, x2, y2, x3, y3)) return true; else if(d4 == 0 && onLine(x1, y1, x2, y2, x4, y4)) return true; else return false; } int find(int pos) { if(pos != set[pos]) set[pos] = find(set[pos]); return set[pos]; } void joinSet(int pos1, int pos2) { if(pos1 == pos2) return; int pre1 = find(pos1); int pre2 = find(pos2); if(pre1 == pre2) return; else { int rank1 = rank[pre1]; int rank2 = rank[pre2]; if(rank1 < rank2) set[pre1] = pre2; else if(rank1 > rank2) set[pre2] = pre1; else { rank[pre1]++; set[pre2] = pre1; } } } void init() { int i; for(i = 1; i <= num; i++) { set[i] = i; rank[i] = 0; } } int main() { int i, n1, n2, pre1, pre2; while(cin>>num && num != 0) { for(i = 1; i <= num; i++) cin>>data[i].x1>>data[i].y1>>data[i].x2>>data[i].y2; init();//n1 = 4, n2 = 5;//if(intersect(data[n1].x1, data[n1].y1, data[n1].x2, data[n1].y2, data[n2].x1, data[n2].y1, data[n2].x2, data[n2].y2))//cout<<"hello"<>n1>>n2 && !(n1 == 0 && n2 == 0)) { pre1 = find(n1); pre2 = find(n2); if(pre1 == pre2) cout<<"CONNECTED"<

좋은 웹페이지 즐겨찾기