您的位置:首页 > 其它

poj 1693 模拟

2016-07-18 15:49 288 查看
Counting Rectangles

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1072 Accepted: 557
Description

We are given a figure consisting of only horizontal and vertical line segments. Our goal is to count the number of all different rectangles formed by these segments. As an example, the number of rectangles in the Figures 1 and 2 are 5 and 0 respectively. 



There are many intersection points in the figure. An intersection point is a point shared by at least two segments. The input line segments are such that each intersection point comes from the intersection of exactly one horizontal segment and one vertical
segment.
Input

The first line of the input contains a single number M, which is the number of test cases in the file (1 <= M <= 10), and the rest of the file consists of the data of the test cases. Each test case begins with a line containing s (1 <= s <= 100), the number
of line segments in the figure. It follows by s lines, each containing x and y coordinates of two end points of a segment respectively. The coordinates are integers in the range of 0 to 1000.
Output

The output for each test case is the number of all different rectangles in the figure described by the test case. The output for each test case must be written on a separate line.
Sample Input
2
6
0 0 0 20
0 10 25 10
20 10 20 20
0 0 10 0
10 0 10 20
0 20 20 20
3
5 0 5 20
15 5 15 25
0 10 25 10

Sample Output
5
0


题意:就是给出一些平行于坐标的线,求围成的矩形

题解:就是随机抽取纵线,然后用横线去截取,可以自己用草稿纸推出公式temp*(temp-1)/ 2

            注意上叙随机二字就好了

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

struct X
{
int x;
int y1,y2;
}vert[105];
int num1;

struct Y
{
int y;
int x1,x2;
}horiz[105];
int num2;

int main()
{
int m,n;
int t1_x,t2_x,t1_y,t2_y;
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
num1=num2=0;
while(n--){
scanf("%d%d%d%d",&t1_x,&t1_y,&t2_x,&t2_y);
if(t1_x==t2_x){
vert[num1].x=t1_x;
vert[num1].y1=min(t1_y,t2_y);
vert[num1++].y2=max(t1_y,t2_y);
}
else{
horiz[num2].y=t1_y;
horiz[num2].x1=min(t1_x,t2_x);
horiz[num2++].x2=max(t1_x,t2_x);
}
}

int ans=0,temp;
for(int i=0;i<num1-1;i++){
for(int j=i+1;j<num1;j++){
temp=0;
if((vert[i].y1>vert[j].y2)||(vert[i].y2<vert[j].y1))
continue;
int max_y=min(vert[i].y2,vert[j].y2);
int min_y=max(vert[i].y1,vert[j].y1);
int min_x=min(vert[i].x,vert[j].x);
int max_x=max(vert[i].x,vert[j].x);

for(int k=0;k<num2;k++){
if(horiz[k].y<=max_y&&horiz[k].y>=min_y)
if(horiz[k].x1<=min_x&&horiz[k].x2>=max_x)
temp++;
}
ans+=temp*(temp-1)/2;
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: