HDU 5762:曼哈顿距离

本文介绍了一道关于曼哈顿距离的算法题目,主要内容是如何判断在给定点集中是否存在两对不同的点,使得它们之间的曼哈顿距离相等。通过哈希表的应用,有效地解决了这一问题,并附上了C++实现的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
Teacher BoBo is a geography teacher in the school.One day in his class,he marked N points in the map,the i -th point is at (Xi,Yi) .He wonders,whether there is a tetrad (A,B,C,D)(A<B,C<D,ACorBD) such that the manhattan distance between A and B is equal to the manhattan distance between C and D.

If there exists such tetrad,print "YES",else print "NO".
 

Input
First line, an integer T . There are T test cases. (T50)

In each test case,the first line contains two intergers, N, M, means the number of points and the range of the coordinates.
(N,M105) .

Next N lines, the
i -th line shows the coordinate of the i -th point. (Xi,Yi)(0Xi,YiM) .
 

Output
T lines, each line is "YES" or "NO".
 

Sample Input
   
2 3 10 1 1 2 2 3 3 4 10 8 8 2 3 3 3 4 4
 
Sample Output
    
YES NO

分析:这个题主要是要知道曼哈顿距离的意思,曼哈顿距离(Manhattan Distance)是由十九世纪的赫尔曼·闵可夫斯基所创词汇 ,是种使用在几何度量空间的几何学用语,用以标明两个点上在标准坐标系上的绝对轴距总和。然后hash一下就解决了。

AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string.h>
#include<algorithm>
using namespace std;
struct Point
{
    int x;
    int y;
};
Point point[100010];
bool ans[10000000];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int N,M;
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N;i++)
            scanf("%d%d",&point[i].x,&point[i].y);
        memset(ans,0,sizeof(ans));
        int flag=0;
        for(int i=1;i<=N-1;i++)
        {
            for(int j=i+1;j<=N;j++)
            {
                int dis=abs(point[i].x-point[j].x)+abs(point[i].y-point[j].y);
                if(ans[dis])
                {
                    flag=1;
                    cout<<"YES"<<endl;
                    break;
                }
                ans[dis]=true;
            }
            if(flag)
                break;
        }
        if(flag==0)
            cout<<"NO"<<endl;
    }
    return 0;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值