After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a rectangle in his backyard. He has set up coordinate axes, and he wants the sides of the rectangle to be parallel to them. Of course, the area of the rectangle must be positive. Wilbur had all four vertices of the planned pool written on a paper, until his friend came along and erased some of the vertices.
Now Wilbur is wondering, if the remaining n vertices of the initial rectangle give enough information to restore the area of the planned swimming pool.
The first line of the input contains a single integer n (1 ≤ n ≤ 4) — the number of vertices that were not erased by Wilbur's friend.
Each of the following n lines contains two integers xi and yi ( - 1000 ≤ xi, yi ≤ 1000) —the coordinates of the i-th vertex that remains. Vertices are given in an arbitrary order.
It's guaranteed that these points are distinct vertices of some rectangle, that has positive area and which sides are parallel to the coordinate axes.
Print the area of the initial rectangle if it could be uniquely determined by the points remaining. Otherwise, print - 1.
2 0 0 1 1
1
1 1 1
-1
In the first sample, two opposite corners of the initial rectangle are given, and that gives enough information to say that the rectangle is actually a unit square.
In the second sample there is only one vertex left and this is definitely not enough to uniquely define the area.
题意:给出 n (1--4) , 最多4个点的横纵坐标,若这些点可以确定一个矩形的话,求其面积 area ,否则输出 -1。
思路:如果是 n==1 ,肯定确定不了一个矩形的;若 n>1,则取其中最小横坐标minx和最小纵坐标miny,最大横坐标maxx和最大纵坐标maxy,若(minx==maxx || miny==maxy)则不构成矩形,输出 -1 ,其它情况计算矩形面积 area。
注意:minx=miny=inf; maxx=maxy=-inf; (看数据范围定)。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
#ifdef OFFLINE
freopen("t.txt","r",stdin);
#endif
int i, j, k, n;
int x[5], y[5], maxx=-inf, maxy=-inf, minx=inf, miny=inf;
scanf("%d", &n);
if(n==1)
printf("-1\n");
else{
for(i=0;i<n;i++){
scanf("%d %d", &x[i], &y[i]);
minx=min(minx, x[i]);
maxx=max(maxx, x[i]);
miny=min(miny, y[i]);
maxy=max(maxy, y[i]);
}
if(maxx==minx||miny==maxy)
printf("-1\n");
else{
int area=abs(maxx-minx)*abs(maxy-miny);
printf("%d\n", area);
}
}
return 0;
}