Rectangle and Square(判断正方形、矩形)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=42#problem/D
改了N多次之后终于A了,一直在改判断正方形和矩形那,判断正方形时算出六条边再排序,若前四条边相等并且与后两条边满足勾股定理,说明是正方形,
判断矩形时,我先对结构体二级排序,这样四个点有确定的顺序,再用点积判断是否有三个角是直角,是的话就是矩形。
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 6 struct node 7 { 8 int x,y; 9 }point[10]; 10 11 int cmp(const struct node a,const struct node b) 12 { 13 if(a.x == b.x) 14 return a.y < b.y; 15 return a.x < b.x; 16 } 17 int dot(const struct node a, const struct node b,const struct node c, const struct node d) 18 { 19 int ans = (a.x-b.x)*(c.x-d.x) + (a.y-b.y)*(c.y-d.y); 20 if(ans == 0) return 1; 21 return 0; 22 } 23 24 int dis(const struct node a, const struct node b) 25 { 26 return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y); 27 } 28 29 int main() 30 { 31 while(~scanf("%d %d",&point[1].x,&point[1].y)) 32 { 33 bool vis[9] = {false}; 34 for(int i = 2; i <= 8; i++) 35 scanf("%d %d",&point[i].x,&point[i].y); 36 int flag = 0; 37 int cnt; 38 struct node t_point[10]; 39 for(int i = 1; i <= 8; i++) 40 { 41 for(int j = i+1; j <= 8; j++) 42 { 43 for(int k = j+1; k <= 8; k++) 44 { 45 for(int l = k+1; l <= 8; l++) 46 { 47 int distance[10]; 48 distance[0] = dis(point[i],point[j]); 49 distance[1] = dis(point[i],point[k]); 50 distance[2] = dis(point[i],point[l]); 51 distance[3] = dis(point[j],point[k]); 52 distance[4] = dis(point[j],point[l]); 53 distance[5] = dis(point[k],point[l]); 54 sort(distance,distance+6); 55 if( distance[0] == distance[1] && 56 distance[1] == distance[2] && 57 distance[2] == distance[3] && 58 distance[4] == distance[5] && 59 (distance[0] + distance[1] == distance[5])) 60 { 61 flag = 1; 62 vis[i] = true; 63 vis[j] = true; 64 vis[k] = true; 65 vis[l] = true; 66 break; 67 } 68 } 69 if(flag) break; 70 } 71 if(flag) break; 72 } 73 if(flag) break; 74 } 75 if(!flag) 76 printf("NO\n"); 77 78 else 79 { 80 int tmp1[5],tmp2[5],t1 = 0,t2 = 0; 81 cnt = 0; 82 for(int i = 1; i <= 8; i++) 83 { 84 if(!vis[i]) 85 { 86 t_point[cnt++] = point[i]; 87 tmp2[t2++] = i; 88 } 89 else tmp1[t1++] = i; 90 } 91 sort(t_point,t_point+cnt,cmp); 92 93 if(dot(t_point[0],t_point[1],t_point[0],t_point[2]) && 94 dot(t_point[0],t_point[1],t_point[1],t_point[3]) && 95 dot(t_point[0],t_point[2],t_point[2],t_point[3])) 96 { 97 printf("YES\n"); 98 for(int i = 0; i < t1-1; i++) 99 printf("%d ",tmp1[i]); 100 printf("%d\n",tmp1[t1-1]); 101 for(int i = 0; i < t2-1; i++) 102 printf("%d ",tmp2[i]); 103 printf("%d\n",tmp2[t2-1]); 104 } 105 else printf("NO\n"); 106 107 } 108 109 } 110 return 0; 111 }