题意分析:
这道题是说在平面上给你几个点,然后你把这些点连接起来,求这个封闭的图形的周长。因为这些被给出的点是这个图形的顶点,且水平方向的相邻点两两连接,所以水平方向的点事偶数个,垂直方向的点也是这样。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct px{
int x,y;
}s[100010];
int cmpx(struct px a,struct px b) {
if(a.x<b.x) return 1;
if(a.x == b.x&& a.y < b.y) return 1;
return 0;
}
int cmpy(struct px a,struct px b) {
if(a.y<b.y) return 1;
if(a.y == b.y&& a.x < b.x) return 1;
return 0;
}
int main() {
int n,i,sum;
while(cin>>n && n) {
sum=0;
for(i=0; i<n; i++) cin>>s[i].x>>s[i].y;
sort(s,s+n,cmpx);
for(i=0; i<n; i++) {
if(s[i+1].x == s[i].x) {
sum+=s[i+1].y-s[i].y;
i++;
}
}
sort(s,s+n,cmpy);
for(i=0; i<n; i++) {
if(s[i+1].y == s[i].y) {
sum+=s[i+1].x-s[i].x;
i++;
}
}
cout<<"The length of the fence will be "<<sum<<" units."<<endl;
}
return 0;
}