POJ 1127 Jack Straws
2014-02-28 21:39
405 查看
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.
[b]Input[/b]
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.
[b]Output[/b]
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.
[b]Sample Input[/b]
[b]Sample Output[/b]
[b]Source[/b]
East Central North America 1994
给出二维平面上的一些线段,之后有一些询问,判断各对线段是否是连通的
由于线段数比较少,可以先判断一遍每对线段之间是否是直接相连的,之后再用Floyd算法找出两条线段之间是否可以间接相连,最后对每组询问,可以直接O(1)给出答案
判断两条线段是否有交点可以先求出两条线段所在直线的交点,之后再判断交点是否在两条线段上。
在几何问题中,运用向量的内积和外积进行计算是非常方便的。对于二维向量p1=(x1,y1)和p2=(x2,y2),我们定义内积p1·p2=x1*x2+y1*y2,外积p1×p2=x1*y2-y1*x2。要判断点q是否在线段p1-p2上,只要先用外积跟据是否有(p1-q)×(p2-q)=0来判断点q是否在直线p1-p2上,再利用内积根据是否有(p1-q)·(p2-q)≤0来判断点q是否落在p1-p2之间。
此题中还要注意边界情况,数据中可能存在两条线段在同一条直线上(即它们是平行的,或者说是共线的),但它们可能在端点处有公共点,这里我们要通过检查端点是否在另一条线段上来判断
[C++]
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2942 | Accepted: 1331 |
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.
[b]Input[/b]
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.
[b]Output[/b]
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.
[b]Sample Input[/b]
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
[b]Sample Output[/b]
CONNECTED NOT CONNECTED CONNECTED CONNECTED NOT CONNECTED CONNECTED CONNECTED CONNECTED CONNECTED
[b]Source[/b]
East Central North America 1994
给出二维平面上的一些线段,之后有一些询问,判断各对线段是否是连通的
由于线段数比较少,可以先判断一遍每对线段之间是否是直接相连的,之后再用Floyd算法找出两条线段之间是否可以间接相连,最后对每组询问,可以直接O(1)给出答案
判断两条线段是否有交点可以先求出两条线段所在直线的交点,之后再判断交点是否在两条线段上。
在几何问题中,运用向量的内积和外积进行计算是非常方便的。对于二维向量p1=(x1,y1)和p2=(x2,y2),我们定义内积p1·p2=x1*x2+y1*y2,外积p1×p2=x1*y2-y1*x2。要判断点q是否在线段p1-p2上,只要先用外积跟据是否有(p1-q)×(p2-q)=0来判断点q是否在直线p1-p2上,再利用内积根据是否有(p1-q)·(p2-q)≤0来判断点q是否落在p1-p2之间。
此题中还要注意边界情况,数据中可能存在两条线段在同一条直线上(即它们是平行的,或者说是共线的),但它们可能在端点处有公共点,这里我们要通过检查端点是否在另一条线段上来判断
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #define MAX_N 15 using namespace std; double EPS=1e-10; double add(double a,double b) { if(fabs(a+b)<EPS*(fabs(a)+fabs(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; P p[MAX_N],q[MAX_N]; bool g[MAX_N][MAX_N]; 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]; } int main() { while(scanf("%d",&n)&&n) { for(int i=0;i<n;i++) scanf("%lf %lf %lf %lf",&p[i].x,&p[i].y,&q[i].x,&q[i].y); solve(); int a,b; while(scanf("%d %d",&a,&b)&&(a||b)) puts(g[a-1][b-1]?"CONNECTED":"NOT CONNECTED"); } return 0; }
[C++]
相关文章推荐
- 使用PHP转义函数:避免XSS漏洞
- OpenCV 脸部跟踪(2)
- 构造函数初始化列表顺序
- Servlet(二)——相关联对象
- .net和JAVA面向对象,继承有趣的细节
- myeclipse 10 破解教程
- 实现的一些内存辅助操作函数
- vs2010:fatal error LNK1123: 转换到 COFF 期间失败
- Mysql数据库学习(四):常用Mysql C API 介绍和使用、封装一个访问Mysql数据库的类MysqlDB
- 大家快来A水题
- UBIFS分区制作 UBIFS烧写 启动 (转载)
- C++多线程总结
- linuxdeepin 2013 安装Netbeans出现JVM terminated. Exit code=13
- 2013年杰森·斯坦森动作《蜂鸟》720p.BD中英双字幕
- Apache下实现禁止目录浏览
- OpenCV 脸部跟踪(1)
- 求二叉树的深度
- Cython基础--Cython的类
- 嵌入式学习心得
- 用TransitionDrawable实现ImageView渐隐渐显过渡效果,并解决OutOfMemory问题