Rectangle and Square
Rectangle and Square
大意:给你8个点,看里面能不能有一个正方形,一个矩形,如果有,输出YES和正方形点的编号和矩形编号,不能输出NO。
PS:正方形和矩形的判断可以当做模板来使用
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 #include <map> 2 #include <stack> 3 #include <queue> 4 #include <math.h> 5 #include <stdio.h> 6 #include <string.h> 7 #include <iostream> 8 #include <algorithm> 9 #define LL long long 10 using namespace std; 11 #define N 12 13 struct node 14 { 15 int x, y; 16 } a[10]; 17 ///判断三点共线函数 18 int is_g(int x1, int y1, int x2, int y2, int x0, int y0) 19 { 20 return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0); 21 } 22 23 ///矩形判断 24 bool is_c(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) 25 { 26 int a[10]; 27 a[0] = (y2-y1)*(y2-y1) + (x2-x1)*(x2-x1); 28 a[1] = (y3-y2)*(y3-y2) + (x3-x2)*(x3-x2); 29 a[2] = (y4-y3)*(y4-y3) + (x4-x3)*(x4-x3); 30 a[3] = (y1-y4)*(y1-y4) + (x1-x4)*(x1-x4); 31 a[4] = (y3-y1)*(y3-y1) + (x3-x1)*(x3-x1); 32 a[5] = (y4-y2)*(y4-y2) + (x4-x2)*(x4-x2); 33 sort(a, a+6); 34 ///判断是否有三点共线 防止0 0 1 0 2 0 1 2这种特殊数据 35 if(!(is_g(x1, y1, x2, y2, x3, y3) && is_g(x2, y2, x3, y3, x4, y4) && is_g(x1, y1, x2, y2, x4, y4) && is_g(x1, y1, x3, y3, x4, y4))) 36 return false; 37 else if(a[0] == a[1] && a[2] == a[3] && a[4] == a[5] && a[0] + a[2] == a[4]) 38 return true; 39 else 40 return false; 41 } 42 43 ///判断正方形 当然也可以在前面矩形的基础上加上临边相等 44 bool is_z(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) 45 { 46 int a[10]; 47 a[0] = (y2-y1)*(y2-y1) + (x2-x1)*(x2-x1); 48 a[1] = (y3-y2)*(y3-y2) + (x3-x2)*(x3-x2); 49 a[2] = (y4-y3)*(y4-y3) + (x4-x3)*(x4-x3); 50 a[3] = (y1-y4)*(y1-y4) + (x1-x4)*(x1-x4); 51 a[4] = (y3-y1)*(y3-y1) + (x3-x1)*(x3-x1); 52 a[5] = (y4-y2)*(y4-y2) + (x4-x2)*(x4-x2); 53 sort(a, a+6); 54 if(a[0] == a[1] && a[1] == a[2] && a[2] == a[3] && a[4] == a[5] && a[0]*2 == a[5]) 55 return true; 56 else 57 return false; 58 } 59 60 void run() 61 { 62 int t[50], o[50]; 63 while(~scanf("%d%d", &a[0].x, &a[0].y)) 64 { 65 for(int i = 1; i < 8; i++) 66 scanf("%d%d", &a[i].x, &a[i].y); 67 int flag = 0; 68 ///遍历找4个点 69 for(int i = 0; i < 8; i++) 70 { 71 for(int j = i+1; j < 8; j++) 72 { 73 for(int k = j+1; k < 8; k++) 74 { 75 for(int l = k+1; l < 8; l++) 76 { 77 memset(t, 0, sizeof(t)); 78 int ans[10]; 79 if(is_z(a[i].x, a[i].y, a[j].x, a[j].y, a[k].x, a[k].y, a[l].x, a[l].y)) 80 { 81 ans[0] = i+1; 82 ans[1] = j+1; 83 ans[2] = k+1; 84 ans[3] = l+1; 85 t[i] = 1; 86 t[j] = 1; 87 t[k] = 1; 88 t[l] = 1; 89 int z = 0; 90 for(int p = 0; p < 8; p++) 91 { 92 if(!t[p]) 93 { 94 o[z++] = p; 95 } 96 } 97 if(is_c(a[o[0]].x, a[o[0]].y, a[o[1]].x, a[o[1]].y, a[o[2]].x, a[o[2]].y, a[o[3]].x, a[o[3]].y)) 98 { 99 ans[4] = o[0]+1; 100 ans[5] = o[1]+1; 101 ans[6] = o[2]+1; 102 ans[7] = o[3]+1; 103 printf("YES\n"); 104 printf("%d %d %d %d\n%d %d %d %d\n", ans[0], ans[1], ans[2], ans[3], ans[4], ans[5], ans[6], ans[7]); 105 flag = 1; 106 break; 107 } 108 } 109 } 110 if(flag) 111 break; 112 } 113 if(flag) 114 break; 115 } 116 if(flag) 117 break; 118 } 119 if(!flag) 120 printf("NO\n"); 121 } 122 } 123 124 int main(void) 125 { 126 run(); 127 128 return 0; 129 }