HDU 5762 Teacher Bo Manhattan Path

Source: Internet
Author: User

Teacher Bo

Time limit:4000/2000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 1014 Accepted Submission (s): 561


Problem Descriptionteacher BoBo is a geography teacher in the school. One day in his class,he markedNPoints in the Map,theI-th Point was at(Xi,Yi) . He Wonders,whether There is a tetrad(A,B,C,d ) (a <b c< d, A≠c or b≠ d) such that the Manhattan distance between A and B are equal to the Manhattan distance between C and D.

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

Inputfirst line, an integerT. There isTTest cases.(T≤)

In all test case,the first line contains-intergers, N, M, means the number of points and the range of the coordinates .(N,M≤5) .

Next N Lines, theI-th line shows the coordinate of theI-th Point.(xi,yi)(0≤xi,yi≤M) .

Output T lines, each of which is "YES" or "NO".

Sample Input23 101 12 23 34 108 82 33 34 4

Sample Outputyes NO
#include <iostream>#include<stdio.h>#include<Set>#include<math.h>using namespacestd;Const intMaxx =100005;Set<int>hav;intPx[maxx];intPy[maxx];intMain () {intT; scanf ("%d",&t);  while(t--)    {        intn,m;        Hav.clear (); scanf ("%d%d",&n,&m);  for(intI=0; i<n; i++) {scanf ("%d%d", px+i,py+i); }        intflag=0;  for(intI=0; i<n-1; i++)        {             for(intj=i+1; j<n; J + +)            {                intDis=abs (Px[j]-px[i]) +abs (py[j]-Py[i]); if(Hav.count (dis)) {flag=1;  Break; }                Else{hav.insert (dis); }            }            if(flag) { Break; }        }        if(flag) printf ("yes\n"); Elseprintf"no\n"); }    return 0;}
View Code
consider a kind of violence, each enumeration 22 points between the Manhattan distance, and open a bucket record whether each distance has occurred, if an enumeration appears before the distance to lose Yesyes, or lose NONO. Note that the Manhattan distance is only O (M) O (m), according to the pigeon Cage principle, The above algorithm must be stopped within O (M) O (m) steps. So it is possible to live. Time complexity of a set of data O (\min{n^2, M}) O (min{n^2??, M}).

HDU 5762 Teacher Bo Manhattan Path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.