11106 - Rectilinear Polygon
Posted: Mon Oct 02, 2006 3:38 am
how this problem can be solved?
Code: Select all
1
30
2 0
2 4
16 0
8 1
9 1
11 1
10 1
10 4
9 7
5 4
6 7
5 7
5 8
12 10
8 7
6 8
5 12
9 10
10 10
12 12
13 10
13 9
13 7
13 4
16 4
10 9
10 7
11 4
10 2
9 2
Code: Select all
82
And the edges are vertical or horizontal. So, we need even number of points.each vertex is an endpoint of exactly one horizontal edge and one vertical edge
Code: Select all
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include <functional> // For greater<int>( )
#include<cassert>
using namespace std;
struct node{
int x;
int y;
//int num;
};
bool UDgreater(node a,node b){
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
bool UDgreater1(node a,node b){
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
int main(){
int cases,nn;
scanf("%d",&cases);
while(cases--){
scanf("%d",&nn);
vector<node> v;
node n;
//input points
for(int i=0;i<nn;i++){
scanf("%d%d",&n.x,&n.y);
v.push_back(n);
}
// horizonal and vertical lists...
vector<node > H=v;
sort(v.begin(),v.end(),UDgreater);
sort(H.begin(),H.end(),UDgreater1);
f=false;
int ret=0;
for(int i=0;i<nn-1;i+=2){
//pair not found..
if(v[i].x!=v[i+1].x){
printf("-1\n");
f=true;
break;
}
// pair found add y coordinate
ret+=abs(v[i+1].y-v[i].y);
// pair not found..
if(H[i].y!=H[i+1].y){
printf("-1\n");
f=true;
break;
}
// pair found add x coordinate
ret+=abs(H[i+1].x-H[i].x);
}
if(f) continue;
printf("%i\n",ret);
}
return 0;
}
Code: Select all
removed
Well, I also checked with assert, and my code runs fine.vinay wrote:the idea is same with your code too...
so whats wrong with me , is wrong with u....
btw i checked for duplicate points using assert function n it gave runtime error.. doesn't it mean that there r duplicate points?
Code: Select all
1
8
0 0
0 2
2 0
2 2
1 1
1 3
3 1
3 3