Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 17740   Accepted: 6776

Description

A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however,
as a regular octagon also has this property. 



So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x
and y coordinates. 

Input

The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the
points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.

Output

For each test case, print on a line the number of squares one can form from the given stars.

Sample Input

4
1 0
0 1
1 1
0 0
9
0 0
1 0
2 0
0 2
1 2
2 2
0 1
1 1
2 1
4
-2 5
3 7
0 0
5 2
0

Sample Output

1
6
1

题意就是给了很多点,然后要在这些点中找出正方形的数量。和HDU5365一样在于,整点是不可能构成正三角形,正五边形,正六边形的。所以题目要求的还是正四边形即正方形的个数。两道题目代码相同。

将各个点按x y排好序,之后搜索点的时候二分。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct no {
int x;
int y;
}node[1005]; int num; bool cmp(const no& node1, const no& node2)
{
if (node1.x == node2.x)
{
return node1.y < node2.y;
}
else
{
return node1.x < node2.x;
}
} bool binsearch(int x, int y)
{
int left = -1, right = num, mid; while (right - left > 1)
{
mid = (left + right) / 2;
if (node[mid].x == x && node[mid].y == y)
return true;
else
{
if ((node[mid].x == x && node[mid].y < y) || (x > node[mid].x))
{
left = mid;
}
else
{
right = mid;
}
}
}
return false;
} int main()
{
int i, j, pos_x1, pos_y1, pos_x2, pos_y2, ans;
while (cin >> num)
{
if (num == 0)
break;
ans = 0; /*if (num <= 3)
{
cout << 0 << endl;
continue;
}之前一直WA在这里!!!*/
for (i = 0; i < num; i++)
{
scanf("%d%d", &node[i].x, &node[i].y);
}
sort(node, node + num, cmp); for (i = 0; i < num; i++)
{
for (j = i + 1; j < num; j++)
{
pos_x1 = node[i].x + (node[i].y - node[j].y);
pos_y1 = node[i].y - (node[i].x - node[j].x); pos_x2 = node[j].x + (node[i].y - node[j].y);
pos_y2 = node[j].y - (node[i].x - node[j].x); if ((binsearch(pos_x1, pos_y1)) && (binsearch(pos_x2, pos_y2)))
ans++; pos_x1 = node[i].x - (node[i].y - node[j].y);
pos_y1 = node[i].y + (node[i].x - node[j].x); pos_x2 = node[j].x - (node[i].y - node[j].y);
pos_y2 = node[j].y + (node[i].x - node[j].x); if ((binsearch(pos_x1, pos_y1)) && (binsearch(pos_x2, pos_y2)))
ans++;
}
}
cout << ans / 4 << endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ2002 &&HDU5365 判断给定的点中有多少个不同的正方形的更多相关文章

  1. SDUT 2129 树结构练习——判断给定森林中有多少棵树

    树结构练习——判断给定森林中有多少棵树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description  众 ...

  2. javascript判断给定字符串是否是回文

    //判断给定字符串是否是回文     function isPalindrome(word) {         var s = new Stack();         for (var i = 0 ...

  3. 图结构练习——判断给定图是否存在合法拓扑序列(dfs算法(第一个代码),邻接矩阵(前两个代码),邻接表(第三个代码))

    sdut 2140 图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  给定一个有向图 ...

  4. 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内。以及两点间的测距功能

    权声明:本文为博主原创文章,未经博主允许不得转载. 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内.以及两点间的测距功能. 绘制多边形(蓝色) ...

  5. SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...

  6. Leetcode36--->Valid Sudoku(判断给定的数独是否有效)

    题目:给定一个数独,某些部分已经被填上了数字,其余空的地方用‘.’表示:判断给定的数独是否有效: 数独规则: 每一行不能有重复的数字:每一列不能有重复的数字:将数独框划分为三行三列,没9个小方格不能有 ...

  7. SDUT-2140_判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一个有向图,判 ...

  8. [CodeWars][JS]如何判断给定的数字是否整数

    问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...

  9. 利用linq快速判断给定数字是否包含在某个段范围内

    一.需求: 知道某段范围0x0020~0x007F0x00A0~0x017F0x01A0~0x01CF0x01F0~0x01FF0x0210~0x021F0x1EA0~0x1EFF给定一个值,快速判断 ...

随机推荐

  1. SIM800L AT command

    /*********************************************************** AT+ICF==<format> ,<parity> ...

  2. 移动互联网APP测试流程及测试点

    1.2测试周期 测试周期可按项目的开发周期来确定测试时间,一般测试时间为两三周(即15个工作日),根据项目情况以及版本质量可适当缩短或延长测试时间.正式测试前先向主管确认项目排期. 1.3测试资源 测 ...

  3. java 解析json格式数据(转)

    2012-07-30 16:43:54|  分类: java |  标签:java  json  |举报|字号 订阅     有时候我们可能会用到json格式的数据进行数据的传输,那么我们怎么把接收到 ...

  4. 加拿大Assignment写作如何靠第一句话来吸睛?

    最近有留学加拿大的同学咨询我们如何写好assignment的首句,关于话题背景的引入,如何才能自然精妙,让老师对后文充满期待.小编用一句话总结今天的策略:陈述一个跟话题直接相关的事实.“直接相关”,保 ...

  5. 三 保存客户&分页查询&Spring解决延迟加载问题

    保存客户 Struts2通过模型驱动拿到封装对象,调用业务层将对象传递到持久层,持久层调用Hibernate模版将对象持久化到数据库. 持久层代码实现: 分页查询: 创建PageBean实体类,里面有 ...

  6. 2.24 模拟赛 + DIV2 总结

    本来实在是不想打了,后来真的手痒. 晚上发现正进行DIV2然后就打了场,模拟,幸好没参加,逆风.排名2400 Codeforces Round #622 (Div. 2) A题十分钟过了 B题http ...

  7. Adapter之spinner

    前言: 在写代码当中有时候会用到下拉列表,下面我们讲一下spinner 正文: 因为比较简单,和之前的listView很像,所以直接上代码 <Spinner android:layout_wid ...

  8. vue 【 子子组件 => 子组件 => 父组件 】 的事件和参数的传递

    1,子子组件  TodoItem.vue     <template>   <div class="todo-item" :class="{'is-co ...

  9. JS写一个列表跑马灯效果--基于touchslide.js

    先放上效果图: 类似于这样的,在列表中循环添加背景样式的跑马灯效果. 准备引入JS插件: <script type="text/javascript" src="x ...

  10. jenkins#构建并部署springboot的jar包

    0. 前提是有一个可以用的没有问题的Jenkins环境,这是基础 1. 安装publish over ssh 插件,(如果网速太慢,请去github 克隆代码,然后自己构建,然后上传安装此插件) 2. ...