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 file 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

The above input file contains two test cases corresponding to Figures 1 and 2 respectively.

题目大意:给一些水平或竖直的线段,求能组成的矩形的个数。

解题思路:因为题目给的只有垂直和水平的线段,且总线段不超过100.所以我们可以暴力。

  1、任选两根水平的线段,若无水平线段可选,结束。否则,转2

  2、从所有的垂直线段里,找到和这两根水平线段相交的线段,假设有tmp条。转3

  3、对于1步选的两条水平线段,因为有tmp跟垂直线段与其相交,根据推算,可以得知,其能组成的矩形就是(tmp - 1)*tmp / 2 个,将其加进总和里即可。转1

 #include<iostream>
#include<string.h>
using namespace std;
class Rect{
public:
int x1,y1,x2,y2;
void set(int a,int b,int c,int d){
x1=a,y1=b,x2=c,y2=d;
}
};//线段类
bool ok(Rect &a,Rect &b){
return b.y1<=a.y1 && a.y1<=b.y2 && a.x1<=b.x1 && b.x1<=a.x2;
}//判断线段相交
int M;
int s;
Rect rectH[],rectS[];//水平和竖直线段集
int main(){
cin>>M;
while(M--){
cin>>s;
int H=,S=;
for(int i=;i<s;i++){
int x,y,x1,y1;
cin>>x>>y>>x1>>y1;
if(x==x1){
if(y>y1)rectS[S++].set(x1,y1,x,y);
else rectS[S++].set(x,y,x1,y1);
}else{
if(x>x1)rectH[H++].set(x1,y1,x,y);
else rectH[H++].set(x,y,x1,y1);
}//要注意从上到下,从左到右
} int tot=;
for(int i=;i<H-;i++){
for(int j=i+;j<H;j++){//枚举2条横的,统计满足相交的竖着的线段的条数count
int count=;
for(int k=;k<S;k++){
if(ok(rectH[i],rectS[k]) && ok(rectH[j],rectS[k]))
count++;
}
tot+=(count-)*count/;//计算此情况能组成多少
}
}
cout<<tot<<'\n';
}return ;
}

最新文章

  1. 线上bug的解决方案--带来的全新架构设计
  2. NE Upgrade python script. Need to write a Tkinter GUI for it
  3. Windows 7 与 Vmware Ubuntu 15.10_64 共享文件夹
  4. thinkphp模型没继承model报的错
  5. [转]iOS/iphone开发如何为苹果开发者帐号APPID续费
  6. CString-int-string-char-BSTR之间的转换
  7. Linux 修改计算机名
  8. C# 读XML文件
  9. OOP—ECMAScript实现详解
  10. 【转】EditText大小(长宽)的多种设置方式----不错
  11. (转)jQuery插件开发全解析
  12. MinGW介绍与使用
  13. 201521123045java课程设计---定时器
  14. spring 内部工作机制(一)
  15. json数据的转义
  16. Java第9次实验(网络)
  17. windows 下 修改jmeter ServerAgent端口
  18. nginx配置ssl证书后无法访问https
  19. web分页打印
  20. geoserver 添加图层数据

热门文章

  1. SQL Server - select语句练习
  2. sql基础知识(新手必备)
  3. spring注解和xml方式区别详解
  4. oracle SQL查询中间若干条记录
  5. LeetCode:Longest Palindromic Substring 最长回文子串
  6. jquery :checked(过滤选择器) 和 空格:checked(后代选择器)
  7. PHPMyadmin 配置文件详解(配置)
  8. 音痴又音痴的LT (vector)
  9. MVC4下配置log4net 五部曲
  10. JS的循环、复杂运算符