当前位置:网站首页>恒星的正方形问题
恒星的正方形问题
2022-08-01 21:35:00 【chengqiuming】
一 原问题描述
Squares - POJ 2002 - Virtual Judgehttps://vjudge.net/problem/POJ-2002
二 输入和输出
1 输入
输入包含多个测试用例。每个测试用例都以整数 n 开始,表示恒星的数量,接下来的 n 行,每行都包含一颗恒星的 x 和 y 坐标(两个整数)。假设这些横恒星的位置是不同的,并且坐标小于 20000.当 n = 0 时,输入终止。
2 输出
对于每个测试用例,都单行输出恒星形成的正方形数。
三 输入和输出样例
1 输入样例
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
2 输出样例
1
6
1
四 分析
如果枚举 4 个节点会超时,可以任意枚举两个点,然后将另两个点算出来,判断是否在已创建的 hash 表里即可,首先枚举(x1,y1)、(x2、y2)两个点,然后以这两个点为边,将所有的左侧和右侧两个点都枚举一次。有以下两种情况,如下图所示,因为正方形内部的几个三角形是相等的,所以可以推导出正方形的另外两个节点(x3,y3)和(x4,y4)。

左侧两点
x3 = x2 - (y1-y2) y3 = y2 + (x1-x2)
x4 = x1 - (y1-y2) y4 = y1 + (x1-x2)
右侧两点
x3 = x2 + (y1-y2) y3 = y2 - (x1-x2)
x4 = x1 + (y1-y2) y4 = y1 - (x1-x2)
五 算法设计
1 把输入数据放入哈希表
2 根据两个点(x1,y1)、(x2,y2),得到左侧两个点(x3,y3)、(x4、y4),在哈希表中查找这两个点是否存在,如果存在,则 ans++。
3 根据两个点(x1,y1)、(x2,y2),得到左侧两个点(x3,y3)、(x4、y4),在哈希表中查找这两个点是否存在,如果存在,则 ans++。
4 计数时对每个正方形都计数了两次,所以答案除以 2。因为根据两个点(x3,y3)、(x4、y4)也可以得到另外两个点(x1,y1)、(x2、y2).
六 代码
package poj2002;
import java.util.Scanner;
public class Poj2002 {
static int N = 1010;
static int H = 10007;
static int sx[] = new int[N];
static int sy[] = new int[N];
static Node node[] = new Node[N];
static int n;
static int cur;
static int hashTable[] = new int[H];
static long ans;
static {
for (int i = 0; i < node.length; i++) {
node[i] = new Node();
}
}
static void initHash() {
for (int i = 0; i < H; i++)
hashTable[i] = -1;
cur = 0;
ans = 0;
}
static void insertHash(int x, int y) {
int h = (x * x + y * y) % H;
node[cur].x = x;
node[cur].y = y;
node[cur].next = hashTable[h];
hashTable[h] = cur++;
}
static boolean searchHash(int x, int y) {
int h = (x * x + y * y) % H;
int next = hashTable[h];
while (next != -1) {
if (x == node[next].x && y == node[next].y)
return true;
next = node[next].next;
}
return false;
}
public static void main(String[] args) {
int xx, yy, x1, y1, x2, y2;
while (true) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
if (n == 0) {
return;
}
initHash();
for (int i = 0; i < n; i++) {
sx[i] = scanner.nextInt();
sy[i] = scanner.nextInt();
insertHash(sx[i], sy[i]);
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
xx = sx[i] - sx[j];
yy = sy[i] - sy[j];
x1 = sx[i] - yy;
y1 = sy[i] + xx;
x2 = sx[j] - yy;
y2 = sy[j] + xx;
if (searchHash(x1, y1) && searchHash(x2, y2))
ans++;
x1 = sx[i] + yy;
y1 = sy[i] - xx;
x2 = sx[j] + yy;
y2 = sy[j] - xx;
if (searchHash(x1, y1) && searchHash(x2, y2))
ans++;
}
}
System.out.println(ans >>= 2);
}
}
}
class Node {
int x;
int y;
int next;
}七 测试
绿色为输入,白色为输出

边栏推荐
- TP5-NPs负载噻吩类化合物TP5白蛋白纳米粒/阿魏酸钠新糖牛血清蛋白纳米粒
- C Pitfalls and Defects Chapter 7 Portability Defects 7.8 Size of Random Numbers
- 0DFS中等 LeetCode6134. 找到离给定两个节点最近的节点
- WEB渗透之SQL 注入
- 【Jmeter常用断言组件】
- 可视化——Superset使用
- SQL injection of WEB penetration
- Appendix A printf, varargs and stdarg A.3 stdarg.h ANSI version of varargs.h
- PyQt5 + MySQL5.8 【学生信息管理系统】【增删改查】
- JSD - 2204 - Knife4j framework - processing - Day07 response results
猜你喜欢

基于php酒店在线预定管理系统获取(php毕业设计)
![[Chinese tree tags - CTB]](/img/f4/b985da2ff83b2a9ab4eebb8bd060bf.png)
[Chinese tree tags - CTB]

LeetCode952三部曲之二:小幅度优化(137ms -> 122ms,超39% -> 超51%)

基于php动漫周边商城管理系统(php毕业设计)

找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!

Based on php online examination management system acquisition (php graduation design)

如何优雅的性能调优,分享一线大佬性能调优的心路历程

Pagoda application experience

2022牛客多校联赛第五场 题解

WEB 渗透之端口协议
随机推荐
教你VSCode如何快速对齐代码、格式化代码
Scala practice questions + answers
ModuleNotFoundError: No module named ‘yaml‘
C pitfalls and pitfalls Chapter 7. Portability pitfalls 7.10 Free first, then realloc
数据库练习
MySQL related knowledge
Popular explanation: what is a clinical prediction model
Based on php film and television information website management system acquisition (php graduation design)
Shell programming conditional statement
Based on php online examination management system acquisition (php graduation design)
AI应用第一课:支付宝刷脸登录
磷酸化甘露糖苷修饰白蛋白纳米粒/卵白蛋白-葡聚糖纳米凝胶的
render-props和高阶组件
C Pitfalls and pitfalls Appendix B Interview with Koenig and Moo
P7215 [JOISC2020] 首都 题解
Day33 LeetCode
365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号
Image fusion GANMcC study notes
上传markdown文档到博客园
模拟数据之mockjs