ACM POJ 1127

Hzr 2011-07-17 04:38:45
Jack Straws
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2154 Accepted: 968
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
#include<iostream>
using namespace std;
//#define MIN(x,y) ( ( x ) < ( y ) ? ( x ) : ( y ) )
//#define MAX(x,y) ( ( x ) > ( y ) ? ( x ) : ( y ) )
int Father[100];
int Rank[100];
int MIN(int a,int b)
{
return a < b ? a : b;
}
int MAX(int a,int b)
{
return a > b ? a : b;
}
struct Point
{
int x,y;
}M[100][2];
int cross(Point O,Point A,Point B)
{
int x1 = A.x - O.x;
int y1 = A.y - O.y;
int x2 = B.x - O.x;
int y2 = B.y - O.y;
return (x1 * y2 - y1 * x2);
}
bool Is_Inter(Point &A,Point &B,Point &C,Point &D)
{
return
(MIN(A.x,B.x) <= MAX(C.x,D.x))
&& (MIN (C.x,D.x) <= MAX(A.x,B.x))
&& (MIN(A.y,B.y) <= MAX(C.y,D.y))
&& (MIN(C.y,D.y) <= MAX(A.y,B.y))
&& (cross(A,C,B) * cross(A,B,D) >= 0)
&& (cross(C,A,D) * cross(A,D,B) >= 0);

}
int Get_Father(int x)
{
if (x != Father[x])
{
Father[x] = Get_Father(Father[x]);
}
return Father[x];
}
void Union(int a,int b)
{
int fa,fb;
fa = Get_Father(a);
fb = Get_Father(b);
if(fa != fb)
{
if(Rank[fa] > Rank[fb]) Father[fb] = fa;
else
{
Father[fa] = fb;
if(Rank[fa] == Rank[fb])
Rank[fb]++;
}
}
}
bool Connected(int a,int b)
{
int fa = Get_Father(a);
int fb = Get_Father(b);
if(fa == fb) return true;
else return false;
}
int main()
{
int n;
while(scanf("%d",&n))
{
if(n == 0) break;
memset(Rank,0,sizeof(Rank));
int i;
for(i=0;i<n;i++) Father[i] = i;
for(i=0;i<n;i++)
scanf("%d %d %d %d",&M[i][0].x,&M[i][0].y,&M[i][1].x,&M[i][1].y);
int j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(Is_Inter(M[i][0],M[i][1],M[j][0],M[j][1]) && i != j)
Union(i,j);
int a,b;
while(scanf("%d %d",&a,&b))
{
if(a == 0 && b == 0) break;
if(Connected(a-1,b-1)) printf("CONNECTED\n");
else printf("NOT CONNECTED\n");

}
}
// system("pause");
return 0;
}



求2直线是否间接相交 搞不清到底哪里WA了
...全文
80 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hzr 2011-07-17
  • 打赏
  • 举报
回复
汗 好像会贴代码了
Hzr 2011-07-17
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;
//#define MIN(x,y) ( ( x ) < ( y ) ? ( x ) : ( y ) )
//#define MAX(x,y) ( ( x ) > ( y ) ? ( x ) : ( y ) )
int Father[100];
int Rank[100];
int MIN(int a,int b)
{
return a < b ? a : b;
}
int MAX(int a,int b)
{
return a > b ? a : b;
}
struct Point
{
int x,y;
}M[100][2];
int cross(Point O,Point A,Point B)
{
int x1 = A.x - O.x;
int y1 = A.y - O.y;
int x2 = B.x - O.x;
int y2 = B.y - O.y;
return (x1 * y2 - y1 * x2);
}
bool Is_Inter(Point &A,Point &B,Point &C,Point &D)
{
return
(MIN(A.x,B.x) <= MAX(C.x,D.x))
&& (MIN (C.x,D.x) <= MAX(A.x,B.x))
&& (MIN(A.y,B.y) <= MAX(C.y,D.y))
&& (MIN(C.y,D.y) <= MAX(A.y,B.y))
&& (cross(A,C,B) * cross(A,B,D) >= 0)
&& (cross(C,A,D) * cross(A,D,B) >= 0);

}
int Get_Father(int x)
{
if (x != Father[x])
{
Father[x] = Get_Father(Father[x]);
}
return Father[x];
}
void Union(int a,int b)
{
int fa,fb;
fa = Get_Father(a);
fb = Get_Father(b);
if(fa != fb)
{
if(Rank[fa] > Rank[fb]) Father[fb] = fa;
else
{
Father[fa] = fb;
if(Rank[fa] == Rank[fb])
Rank[fb]++;
}
}
}
bool Connected(int a,int b)
{
int fa = Get_Father(a);
int fb = Get_Father(b);
if(fa == fb) return true;
else return false;
}
int main()
{
int n;
while(scanf("%d",&n))
{
if(n == 0) break;
memset(Rank,0,sizeof(Rank));
int i;
for(i=0;i<n;i++) Father[i] = i;
for(i=0;i<n;i++)
scanf("%d %d %d %d",&M[i][0].x,&M[i][0].y,&M[i][1].x,&M[i][1].y);
int j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(Is_Inter(M[i][0],M[i][1],M[j][0],M[j][1]) && i != j)
Union(i,j);
int a,b;
while(scanf("%d %d",&a,&b))
{
if(a == 0 && b == 0) break;
if(Connected(a-1,b-1)) printf("CONNECTED\n");
else printf("NOT CONNECTED\n");

}
}
// system("pause");
return 0;
}
Hzr 2011-07-17
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
//#define MIN(x,y) ( ( x ) < ( y ) ? ( x ) : ( y ) )
//#define MAX(x,y) ( ( x ) > ( y ) ? ( x ) : ( y ) )
int Father[100];
int Rank[100];
int MIN(int a,int b)
{
return a < b ? a : b;
}
int MAX(int a,int b)
{
return a > b ? a : b;
}
struct Point
{
int x,y;
}M[100][2];
int cross(Point O,Point A,Point B)
{
int x1 = A.x - O.x;
int y1 = A.y - O.y;
int x2 = B.x - O.x;
int y2 = B.y - O.y;
return (x1 * y2 - y1 * x2);
}
bool Is_Inter(Point &A,Point &B,Point &C,Point &D)
{
return
(MIN(A.x,B.x) <= MAX(C.x,D.x))
&& (MIN (C.x,D.x) <= MAX(A.x,B.x))
&& (MIN(A.y,B.y) <= MAX(C.y,D.y))
&& (MIN(C.y,D.y) <= MAX(A.y,B.y))
&& (cross(A,C,B) * cross(A,B,D) >= 0)
&& (cross(C,A,D) * cross(A,D,B) >= 0);

}
int Get_Father(int x)
{
if (x != Father[x])
{
Father[x] = Get_Father(Father[x]);
}
return Father[x];
}
void Union(int a,int b)
{
int fa,fb;
fa = Get_Father(a);
fb = Get_Father(b);
if(fa != fb)
{
if(Rank[fa] > Rank[fb]) Father[fb] = fa;
else
{
Father[fa] = fb;
if(Rank[fa] == Rank[fb])
Rank[fb]++;
}
}
}
bool Connected(int a,int b)
{
int fa = Get_Father(a);
int fb = Get_Father(b);
if(fa == fb) return true;
else return false;
}
int main()
{
int n;
while(scanf("%d",&n))
{
if(n == 0) break;
memset(Rank,0,sizeof(Rank));
int i;
for(i=0;i<n;i++) Father[i] = i;
for(i=0;i<n;i++)
scanf("%d %d %d %d",&M[i][0].x,&M[i][0].y,&M[i][1].x,&M[i][1].y);
int j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(Is_Inter(M[i][0],M[i][1],M[j][0],M[j][1]) && i != j)
Union(i,j);
int a,b;
while(scanf("%d %d",&a,&b))
{
if(a == 0 && b == 0) break;
if(Connected(a-1,b-1)) printf("CONNECTED\n");
else printf("NOT CONNECTED\n");

}
}
// system("pause");
return 0;
}

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧