我WA,他AC,无语…………………………………………………………………………………………
我的代码:
#include<iostream> #include<cmath> #define N 35 #define K 105 using namespace std; class cor{ public: int x,y; }; int drection(cor& a,cor& b,cor& c){ return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); } bool on_segment(cor& a,cor& b,cor& c){ return (min(a.x,b.x) <= c.x && c.x <= max(a.x,b.x))&&((min(a.y,b.y) <= c.y)&&(c.y <= max(a.y,b.y))); } bool judge(cor& p1,cor& p2,cor& p3,cor& p4){ int d1 = drection(p1,p2,p3); int d2 = drection(p1,p2,p4); int d3 = drection(p3,p4,p1); int d4 = drection(p3,p4,p2); if((d1*d2 < 0) && (d3*d4 < 0)) return true; else if(!d1 && on_segment(p1,p2,p3)) return true; else if(!d2 && on_segment(p1,p2,p4)) return true; else if(!d3 && on_segment(p3,p4,p1)) return true; else if(!d4 && on_segment(p3,p4,p2)) return true; else return false; } int main() { cor pipe[N][K]; int k[K]; int n; while(cin >>n){ bool flag = true; for(int i = 0;i < n;i++){ cin >>k[i]; for(int j = 0;j < k[i];j++){ cin >>pipe[i][j].x >>pipe[i][j].y; } } for(int i = 0;i < n;i++){ for(int j = i+1;j < n;j++){ for(int s = 0;s < k[i]-1;s++){ for(int t = 0;t < k[j]-1;t++){ if(judge(pipe[i][s],pipe[i][s+1],pipe[j][t],pipe[j][t+1])){ flag = false; break; } } } } } if(flag) cout <<"No" <<endl; else cout <<"Yes" <<endl; } return 0; }
他的代码:
#include<stdio.h> struct pipe{ int point; int x[101]; int y[101]; }a[30]; int max(int a,int b) { if(a>b) {return a;} else {return b;} } int min(int a,int b) { if(a>b) {return a;} else {return b;} } int dic(int x1,int y1,int x2,int y2,int x3,int y3) { int n; n=(x3-x1)*(y2-y1)-(x2-x1)*(y3-y1); return n; } int onse(int xi,int yi,int xj,int yj,int xk,int yk) { if((min(xi,xj)<=xk&&xk<=max(xi,xj))&&(min(yi,yj)<=yk&&yk<=max(yi,yj))) {return 1;} else {return 0;} } int cross(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4) { int d1,d2,d3,d4; d1=dic(x3,y3,x4,y4,x1,y1); d2=dic(x3,y3,x4,y4,x2,y2); d3=dic(x1,y1,x2,y2,x3,y3); d4=dic(x1,y1,x2,y2,x4,y4); if(((d1>0&&d2<0)||(d1<0&&d2>0))&&((d3>0&&d4<0)||(d3<0&&d4>0))) {return 1;} else if(d1==0&&onse(x3,y3,x4,y4,x1,y1)) {return 1;} else if(d2==0&&onse(x3,y3,x4,y4,x2,y2)) {return 1;} else if(d3==0&&onse(x1,y1,x2,y2,x3,y3)) {return 1;} else if(d4==0&&onse(x1,y1,x2,y2,x4,y4)) {return 1;} else {return 0;} } int main(void) { int n,cros; int i,j,k,b; while(scanf("%d",&n)==1) { cros=0; for(i=0;i<n;i++) { scanf("%d",&a[i].point); for(j=0;j<a[i].point;j++) { scanf("%d%d",&a[i].x[j],&a[i].y[j]); } } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { for(k=0;k<a[i].point-1;k++) { for(b=0;b<a[j].point-1;b++) { if(cross(a[i].x[k],a[i].y[k],a[i].x[k+1],a[i].y[k+1],a[j].x[b],a[j].y[b],a[j].x[b+1],a[j].y[b+1])) {cros=1;} } } } } if(cros) { printf("Yes\n");} else { printf("No\n");} } return 0; }