POJ 1127 -- Jack Straws
首先给出一个n,表示共有n条边,下面n行(编号1-n)每行给出4个数分别表示起始点的横纵坐标以及终止点的横纵坐标。接下来的若干行为查询条件,要你输出查询的两条边是否有关系(即直接相交或间接相交)。
处理完所有的边后,查询就简单了,只要看被查询的两条边是否在同一个集合即可。如果在,则有关系;如果不在,则没有关系。
http://blog.csdn.net/stormdpzh/article/details/8738782
Read full article from 1127 -- 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.
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 CONNECTEDhttp://www.slyar.com/blog/poj-1127-c.html
首先给出一个n,表示共有n条边,下面n行(编号1-n)每行给出4个数分别表示起始点的横纵坐标以及终止点的横纵坐标。接下来的若干行为查询条件,要你输出查询的两条边是否有关系(即直接相交或间接相交)。
处理完所有的边后,查询就简单了,只要看被查询的两条边是否在同一个集合即可。如果在,则有关系;如果不在,则没有关系。
http://blog.csdn.net/stormdpzh/article/details/8738782
- const int INF = 1 << 30;
- const int MaxN = 100005;
- const double eps = 1e-9;
- int sgn(double d)
- {
- if(d > eps) return 1;
- if(d < -eps) return -1;
- return 0;
- }
- struct Point
- {
- int x, y;
- Point() {}
- Point(int _x, int _y) : x(_x), y(_y) {}
- void read()
- {
- scanf("%d%d", &x, &y);
- }
- };
- Point pnt[15][3];
- int n;
- int a, b;
- bool con[15][15];
- bool vis[15];
- Point operator - (const Point &p1, const Point &p2)
- {
- return Point(p1.x - p2.x, p1.y - p2.y);
- }
- double operator * (const Point &p1, const Point &p2)
- {
- return (double)(p1.x * p2.y - p1.y * p2.x);
- }
- bool get_line_intersection(const Point &p1, const Point &p2, const Point &p3, const Point &p4)
- {
- double d1 = (p2 - p1) * (p3 - p1);
- double d2 = (p2 - p1) * (p4 - p1);
- if(0 == sgn(d1 - d2)) return false;
- return true;
- }
- bool get_segment_intersection(const Point &p1, const Point &p2, const Point &p3, const Point &p4)
- {
- if(!get_line_intersection(p1, p2, p3, p4)) {
- if(min(p1.x, p2.x) > max(p3.x, p4.x) || min(p3.x, p4.x) > max(p1.x, p2.x)) return false;
- if(min(p1.y, p2.y) > max(p3.y, p4.y) || min(p3.y, p4.y) > max(p1.y, p2.y)) return false;
- return true;
- }
- double d1 = (p2 - p1) * (p3 - p1);
- double d2 = (p2 - p1) * (p4 - p1);
- double d3 = (p4 - p3) * (p1 - p3);
- double d4 = (p4 - p3) * (p2 - p3);
- int s1 = sgn(d1), s2 = sgn(d2), s3 = sgn(d3), s4 = sgn(d4);
- if(s1 * s2 > 0 || s3 * s4 > 0 || sgn(d1 - d2) == 0) return false;
- return true;
- }
- bool gao(int x, int y)
- {
- if(con[x][y]) return true;
- vis[x] = true;
- repf(i, 1, n) {
- if(!vis[i] && con[x][i]) {
- vis[i] = true;
- bool f = gao(i, y);
- if(f) return true;
- }
- }
- return false;
- }
- int main()
- {
- while(1 == scanf("%d", &n) && n > 0) {
- repf(i, 1, n) {
- pnt[i][0].read();
- pnt[i][1].read();
- }
- mset(con, false);
- repf(i, 1, n) repf(j, 1, n) {
- if(i == j) con[i][j] = true;
- else con[i][j] = get_segment_intersection(pnt[i][0], pnt[i][1], pnt[j][0], pnt[j][1]);
- }
- while(2 == scanf("%d%d", &a, &b)) {
- if(a == 0 && b == 0) break;
- mset(vis, false);
- if(gao(a, b)) puts("CONNECTED");
- else puts("NOT CONNECTED");
- }
- }
- return 0;
- }