POJ2002 &&HDU5365 判断给定的点中有多少个不同的正方形
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 17740 | Accepted: 6776 |
Description
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
points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.
Output
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 判断给定的点中有多少个不同的正方形的更多相关文章
- SDUT 2129 树结构练习——判断给定森林中有多少棵树
树结构练习——判断给定森林中有多少棵树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 众 ...
- javascript判断给定字符串是否是回文
//判断给定字符串是否是回文 function isPalindrome(word) { var s = new Stack(); for (var i = 0 ...
- 图结构练习——判断给定图是否存在合法拓扑序列(dfs算法(第一个代码),邻接矩阵(前两个代码),邻接表(第三个代码))
sdut 2140 图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定一个有向图 ...
- 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内。以及两点间的测距功能
权声明:本文为博主原创文章,未经博主允许不得转载. 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内.以及两点间的测距功能. 绘制多边形(蓝色) ...
- SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列
数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...
- Leetcode36--->Valid Sudoku(判断给定的数独是否有效)
题目:给定一个数独,某些部分已经被填上了数字,其余空的地方用‘.’表示:判断给定的数独是否有效: 数独规则: 每一行不能有重复的数字:每一列不能有重复的数字:将数独框划分为三行三列,没9个小方格不能有 ...
- SDUT-2140_判断给定图是否存在合法拓扑序列
数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一个有向图,判 ...
- [CodeWars][JS]如何判断给定的数字是否整数
问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...
- 利用linq快速判断给定数字是否包含在某个段范围内
一.需求: 知道某段范围0x0020~0x007F0x00A0~0x017F0x01A0~0x01CF0x01F0~0x01FF0x0210~0x021F0x1EA0~0x1EFF给定一个值,快速判断 ...
随机推荐
- js保留两位小数的数字格式化方法
// 格式化数字(保留两位小数) numberFormat (num) { let percent = Number(num.toString().match(/^\d+(?:\.\d{0,2})?/ ...
- mind map 思维导图
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ch4 圆角框
固定宽度的圆角框 只需要两个图像:一个应用于框的顶部,一个应用于底部 <div class="box"> <h2>Lorem Ipsum</h2> ...
- c/c++ struct的大小以及sizeof用法
直入主题,怎么判断内存对齐规则,sizeof的结果怎么来的,请牢记以下3条原则:(在没有#pragma pack宏的情况下) 1:数据成员对齐规则:结构(struct)(或联合(union))的数据成 ...
- C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
注意: 以下代码,属性直接赋值的语法糖要vs2015以上才支持. using System.ComponentModel; using System.Drawing; using System. ...
- 图的Prim,Kruskal,Dijkstra,Floyd算法
代码部分有点问题,具体算法没问题, 最近期末考,要过段时间才会修改 //邻接矩阵,具体情况看上一篇的图的实现template<class T>class MGraph {public: ...
- 多元线性回归算法的python底层代码编写实现
1.对于多元线性回归算法,它对于数据集具有较好的可解释性,我们可以对比不过特征参数的输出系数的大小来判断它对数据的影响权重,进而对其中隐含的参数进行扩展和收集,提高整体训练数据的准确性. 2.多元回归 ...
- C++面试常见问题——12虚函数
虚函数 虚函数的工作原理 虚函数的实现要求对象携带额外的信息,这些信息用于确定运行时调用哪一个虚函数,这一信息具有一种被称为虚函数表指针(vptr)的指针形式.vptr指向一个被称为虚函数表(vtbl ...
- Linux-10Year
主流Linux发行版近10年排行曲线 10个主流的发行版概述(distrowatch挑选) 搜索特定的发行 选择建议 初级用户选择:开箱即用 中级用户组装:适合你自己的系统 高 ...
- supervisor的介绍
1.supervisor 简介 Supervisor 是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统.它可以很方便的 ...