POJ - 1127 Jack Straws

题目链接

AcCode:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 25;
const int maxm = 100000;
const double EPS = 1e-10;

double add(double a, double b)
{
    if(abs(a+b) < EPS*(abs(a)+abs(b)))
        return 0;
    return a+b;
}

typedef 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);
    }
};

//判断点p 是否在线段p1 - p2上
bool on_seg(P p1, P p2, P q)
{
    return (p1-q).det(p2-q) == 0 && (p1-q).dot(p2-q) <= 0;
}
//求出p1p2, q1q2的交点
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[maxm], b[maxm];
bool g[maxn][maxn];

bool input()
{
    scanf("%d", &n);
    if(n == 0) return false;
    for(int i = 0; i < n; i++)
        scanf("%lf %lf %lf %lf", &p[i].x, &p[i].y, &q[i].x, &q[i].y);
    m = 0;
    int aa, bb;
    while(scanf("%d %d", &aa, &bb)){
        if(aa == 0 && bb == 0) break;
        a[m] = aa, b[m] = bb;
        m++;
    }
    return true;
}

void solve()
{
    for(int i = 0; i < n; i++){
        g[i][i] = true;
        for(int j = 0; 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);  //交点在两条线段上
            }
        }
    }
    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++){
        if(g[a[i]-1][b[i]-1]) cout << "CONNECTED" << endl;
        else                  cout << "NOT CONNECTED" << endl;
    }
}

int main()
{
    while(input()){
        solve();
    }
    return 0;
}


Jack Straws

07-27

DescriptionnnIn 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.nInputnnInput 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. nnWhen n=0,the input is terminated. nnThere will be no illegal input and there are no zero-length straws. nOutputnnYou 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.nSample Inputnn7n1 6 3 3 n4 6 4 9 n4 5 6 7 n1 4 3 5 n3 5 5 5 n5 2 6 3 n5 4 7 2 n1 4 n1 6 n3 3 n6 7 n2 3 n1 3 n0 0nn2n0 2 0 0n0 0 0 1n1 1n2 2n1 2n0 0nn0nSample OutputnnCONNECTED nNOT CONNECTED nCONNECTED nCONNECTED nNOT CONNECTED nCONNECTEDnCONNECTEDnCONNECTEDnCONNECTED

ACM POJ 1127

07-17

Jack StrawsrnTime Limit: 1000MS Memory Limit: 10000KrnTotal Submissions: 2154 Accepted: 968rnDescriptionrnrnIn 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.rnInputrnrnInput 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. rnrnWhen n=0,the input is terminated. rnrnThere will be no illegal input and there are no zero-length straws. rnOutputrnrnYou 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.rnSample Inputrnrn7rn1 6 3 3 rn4 6 4 9 rn4 5 6 7 rn1 4 3 5 rn3 5 5 5 rn5 2 6 3 rn5 4 7 2 rn1 4 rn1 6 rn3 3 rn6 7 rn2 3 rn1 3 rn0 0rnrn2rn0 2 0 0rn0 0 0 1rn1 1rn2 2rn1 2rn0 0rnrn0rnSample OutputrnrnCONNECTED rnNOT CONNECTED rnCONNECTED rnCONNECTED rnNOT CONNECTED rnCONNECTEDrnCONNECTEDrnCONNECTEDrnCONNECTEDrn#includernusing namespace std;rn//#define MIN(x,y) ( ( x ) < ( y ) ? ( x ) : ( y ) )rn//#define MAX(x,y) ( ( x ) > ( y ) ? ( x ) : ( y ) )rnint Father[100];rnint Rank[100];rnint MIN(int a,int b)rnrn return a < b ? a : b;rnrnint MAX(int a,int b)rnrn return a > b ? a : b;rnrnstruct Pointrnrn int x,y;rnM[100][2];rnint cross(Point O,Point A,Point B)rnrn int x1 = A.x - O.x;rn int y1 = A.y - O.y;rn int x2 = B.x - O.x;rn int y2 = B.y - O.y;rn return (x1 * y2 - y1 * x2);rnrnbool Is_Inter(Point &A,Point &B,Point &C,Point &D)rnrn returnrn (MIN(A.x,B.x) <= MAX(C.x,D.x))rn && (MIN (C.x,D.x) <= MAX(A.x,B.x))rn && (MIN(A.y,B.y) <= MAX(C.y,D.y))rn && (MIN(C.y,D.y) <= MAX(A.y,B.y)) rn && (cross(A,C,B) * cross(A,B,D) >= 0)rn && (cross(C,A,D) * cross(A,D,B) >= 0);rn rnrnint Get_Father(int x)rnrn if (x != Father[x])rn rn Father[x] = Get_Father(Father[x]);rn rn return Father[x];rnrnvoid Union(int a,int b) rnrn int fa,fb;rn fa = Get_Father(a);rn fb = Get_Father(b);rn if(fa != fb)rn rn if(Rank[fa] > Rank[fb]) Father[fb] = fa;rn elsern rn Father[fa] = fb;rn if(Rank[fa] == Rank[fb])rn Rank[fb]++;rn rn rnrnbool Connected(int a,int b)rnrn int fa = Get_Father(a);rn int fb = Get_Father(b);rn if(fa == fb) return true;rn else return false;rnrnint main()rnrn int n;rn while(scanf("%d",&n))rn rn if(n == 0) break;rn memset(Rank,0,sizeof(Rank)); rn int i;rn for(i=0;i

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭