G | Determine the Shape | |
Input | Standard Input | |
Output | Standard Output |
A toy company recently found that toys like revolver, machine guns, fighting planes are making children violent and destroying the peace of the world. The parents also began to avoid these toys and inclined to educational toys. So they decided to manufacture educational toys. One of these is a electric touch pad on which children can put four points and the program will automatically join the points to form a closed shape. Children will try to guess the shape and when they press a button then it will automatically announce the shape. But they are struggling to determine the shape and seek your help.
Your task is simple. You are given four points, no three of them are collinear, you have to output the simple polygonal shape formed by these points in the following order:
Square
Rectangle
Rhombus
Parallelogram
Trapezium
Ordinary Quadrilateral
For example if it is possible to form a square with the four points you must output ‘Square’, if it is not possible to form a square but possible to form a rectangle you must output ‘Rectangle’ and so on.
Input
Input starts with an integer T, the number of test cases (T≤50000). Each test case contains 4 lines. Each of the lines contains two space separated integers xi yi (-10000≤xi, yi≤ 10000) which are the coordinate values of a point.
Output
For each set of input output one line in the format “Case k: s”. Here k is the case number starting from 1 and s is the shape as described above. See sample input output for more details.
Sample Input | Sample Output |
6 0 0 2 0 2 2 0 2 0 0 3 0 3 2 0 2 0 0 8 4 5 0 3 4 0 0 2 0 3 2 1 2 0 0 5 0 4 3 1 3 0 0 5 0 4 3 1 4
| Case 1: Square Case 2: Rectangle Case 3: Rhombus Case 4: Parallelogram Case 5: Trapezium Case 6: Ordinary Quadrilateral
|
题意:
给出 4 个点的坐标,输出这是 正方形,长方形,平行四边形,梯形,普通三角形 还是 菱形。
思路:
暴力。输出的坐标可以先预处理一下,x 轴小到大排序,再 y 小到大排序。对于正方形,长方形,平行四边形,菱形都是可以直接判断的,但是梯形的话就要判断任意两条边平行,剩下两条边不平行来判断。如果全都不是,则说明是普通三角形。
AC:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef struct {
ll x, y;
}node;
ll len(ll x1, ll y1, ll x2, ll y2) {
ll x = (x2 - x1) * (x2 - x1);
ll y = (y2 - y1) * (y2 - y1);
return (x + y);
}
bool ang(node a, node b, node c, node d) {
ll x1 = a.x - b.x, y1 = a.y - b.y;
ll x2 = c.x - d.x, y2 = c.y - d.y;
if (x1 * x2 + y1 * y2 == 0) return true;
return false;
}
bool par(node a, node b, node c, node d) {
ll x1 = a.x - b.x, y1 = a.y - b.y;
ll x2 = c.x - d.x, y2 = c.y - d.y;
if (x1 * y2 - x2 * y1 == 0) return true;
return false;
}
bool Trapezium(node a, node b, node c, node d) {
if (par(a, b, c, d) && !par(a, c, b, d)) return true;
if (par(a, c, b, d) && !par(a, b, c, d)) return true;
if (par(b, c, d, a) && !par(b, d, c, a)) return true;
return false;
}
bool cmp(node a, node b) {
if (a.x != b.x) return a.x < b.x;
return a.y < b.y;
}
int main() {
int t;
scanf("%d", &t);
for (int tt = 1; tt <= t; ++tt) {
node no[5];
for (int i = 1; i <= 4; ++i)
scanf("%lld%lld", &no[i].x, &no[i].y);
sort(no + 1, no + 5, cmp);
ll len1 = len(no[1].x, no[1].y, no[2].x, no[2].y);
ll len2 = len(no[1].x, no[1].y, no[3].x, no[3].y);
ll len3 = len(no[2].x, no[2].y, no[4].x, no[4].y);
ll len4 = len(no[3].x, no[3].y, no[4].x, no[4].y);
printf("Case %d: ", tt);
if (len1 == len2 && len3 == len4 && len1 == len4 &&
ang(no[2], no[1], no[3], no[1]) &&
ang(no[1], no[2], no[4], no[2]) &&
ang(no[1], no[3], no[4], no[3]) &&
ang(no[2], no[4], no[3], no[4]) ) printf("Square\n");
else if (len1 == len4 && len2 == len3 && len1 != len2 &&
ang(no[2], no[1], no[3], no[1]) &&
ang(no[1], no[2], no[4], no[2]) &&
ang(no[1], no[3], no[4], no[3]) &&
ang(no[2], no[4], no[3], no[4]) ) printf("Rectangle\n");
else if (len1 == len4 && len2 == len3 && len1 == len2 &&
ang(no[3], no[2], no[4], no[1])) printf("Rhombus\n");
else if (par(no[4], no[2], no[3], no[1]) &&
par(no[2], no[1], no[4], no[3])) printf("Parallelogram\n");
else if (Trapezium(no[1], no[2], no[3], no[4])) printf("Trapezium\n");
else printf("Ordinary Quadrilateral\n");
}
return 0;
}