Jack Straws
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
|
题意是给你n个木棒,告诉你他们的坐标,然后有m个询问,问你p[i]木棒和p[j]木棒是否直接或间接相连。
思路:先去搞直接相连,就是平面线段相交,然后Flody扫一遍,传递连接关系。
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> #include <utility> #include <cassert> using namespace std; ///#define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 #define mk make_pair #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) const int MAXN = 15 + 5; const int MAXS = 10000 + 50; const int sigma_size = 26; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; const int inf = 1 << 30; #define eps 1e-10 const long long MOD = 1000000000 + 7; const int mod = 10007; typedef long long LL; const double PI = acos(-1.0); typedef double D; typedef pair<int , int> pii; typedef vector<int> vec; typedef vector<vec> mat; #define Bug(s) cout << "s = " << s << endl; ///#pragma comment(linker, "/STACK:102400000,102400000") ///考虑误差的加法运算 double add(double a , double b) { if(abs(a + b) < eps * (abs(a) + abs(b)))return 0; return a + b; } ///二维向量结构体 struct P { double x , y; P(){} P(double x , double y) : x(x) , y(y){} P operator + (P p) { return P(add(x , p.x) , add(y , p.y)); } P operator - (P p) { return P(add(x , - p.x) , add(y , - p.y)); } P operator * (double d) { return P(x * d , y * d); } double dot(P p)///点乘 { return add(x * p.x , y * p.y); } double det(P p)///叉乘 { return add(x * p.y , - y * p.x); } }; ///判断点q是否在线段p1-p2上 bool on_seg(P p1 , P p2 , P q) { return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0; } ///计算直线p1-p2与直线q1-q2的交点坐标 P intersection(P p1 , P p2 , P q1 , P q2) { return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1)); } int n , m; P p[MAXN] , q[MAXN]; int a[MAXS] , b[MAXS]; bool g[MAXN][MAXN]; void solve() { for(int i = 0 ; i < n ; i++) { g[i][i] = true; for(int j = 0 ; j < i ; j++) { ///判断木棒i和j是否有公共点 if((p[i] - q[i]).det(p[j] - q[j]) == 0) { ///平行 g[i][j] = g[j][i] = on_seg(p[i] , q[i] , p[j]) || on_seg(p[i] , q[i] , q[j]) || on_seg(p[j] , q[j] , p[i]) || on_seg(p[j] , q[j] , q[i]); } else { ///非平行 P r = intersection(p[i] , q[i] , p[j] , q[j]); g[i][j] = g[j][i] = on_seg(p[i] , q[i] , r) && on_seg(p[j] , q[j] , r); } } ///Floyd-Warshall算法判断任意两点间是否相连 for(int k = 0 ; k < n ; k++) { for(int i = 0 ; i < n ; i++) { for(int j = 0 ; j < n ; j++) { g[i][j] |= g[i][k] && g[k][j]; } } } } for(int i = 0 ; i < m; i++) { puts(g[a[i] - 1][b[i] - 1] ? "CONNECTED" : "NOT CONNECTED"); } } int main() { while(~scanf("%d" , &n) , n) { clr(g , 0); for(int i = 0 ; i < n ; i ++) { scanf("%lf%lf%lf%lf" , &p[i].x , &p[i].y , &q[i].x , &q[i].y); } int x , y; m = 0; while(scanf("%d%d" , &x , &y) , x || y) { a[m] = x , b[m] = y; m++; } solve(); } return 0; }