Acdream 1681 long jump Queen (BFS)

Source: Internet
Author: User

Problem Description

Nana thought the piano is very uninteresting, abandoned the piano, continue to walk, Front is a lake, Nana thought of the other side of the lake, but Nana looked for a long time did not find the small bridge and boat, Nana also found that they are not immortal, not like eight Immortals crossing. Just when Nana worried, Nana found some rocks above the lake! Nana Brainwave, found that can follow the stone to jump acridine, so keep jumping, perhaps can jump to the other side!

Nana to the location of all the stones to tell you, and then Nana can jump the farthest distance is also know ~ please smart you tell Nana, she can reach the other side smoothly?

In order to be able to express the position of each stone smoothly, assuming that Nana is on the x-axis, the lake on the other side of the bank is a straight line y=y0, the stones in the lake are ordered two-tuple <x,y>, we can assume that the lake is infinite width, two stone distance for the geometric distance, The distance between the stone and the shore is from the point to the straight line.

Input

Multiple sets of data, first a positive integer T (t<=20) representing the number of data groups

For each set of data is first three integers y0 (1<=y0<=1000), N (0<=n<=1000), D (0<=d<=1000), respectively, the location of the lake on the other shore, the number of stones, Nana once the furthest distance to jump.

Next is n rows, each line being two integers x, y (0<=|x|<=1000,0<y<y0)

Output

For each set of data, if Nana can reach another shore of the lake, first output "YES", and then output an integer, which means Nana at least how many times to jump to the other shore,

If Nana cannot reach another shore of the lake, first output "NO", and then output an integer that represents Nana's closest distance from the other shore of the lake. (Note case)

Sample Input
24 3 10 10 20 36 3 20 11 22 3
Sample Output
Yes4no3
Hint

Example one, from the opposite side of the 0,2 (0,3), x-axis (0,1), a total of 4 steps, Output 4

Example two, from the x-axis (0,1)---(2,3), at this time the distance from the other shore is 3, the maximum jump distance of 2, can not reach the other side, so output 3

Test instructions: to the x-axis, jump to the opposite shore yy on, give a number of stones in y=0 and y=yy between, there is stone can stand, each jump has a distance limit, ask whether can reach the other side, if not, how far from the opposite shore?

Idea: Wide search, stone only 1000, with each stone index, each block maintenance a queue, is the stone can jump to the team. In BFS only two places on the takeoff shore and the arrival shore require special treatment. Jumping point directly ignored, take the takeoff shore one step can reach the stone as the starting point to search. Determine if you can reach the opposite shore one step at a time when searching for each point.

1* Problem:16812*verdict:accepted3* Submission Date: -- .- the  -: -: -4*time:180ms5*memory:1984kb6*/7#include <bits/stdc++.h>8 #defineLL Long Long9 using namespacestd;Ten Const intn=1005; One intyy, N, D; A BOOLVis[n]; -   - structnode the { -     intnum; -     intx, y; - }a[n]; +   -vector< vector< Node > >Vect; +deque< Node >que; A intcal () at { - que.clear (); -     intCnt=0, up=0; -      for(intI=0; i<n; i++)//take the first queue as a starting point, i.e. the one-step-up stone from the takeoff shore -     { -         if(a[i].y<=d) in         { -up=Max (UP,A[I].Y); to Que.push_back (A[i]); +vis[a[i].num]=1;//have browsed -Cnt=1; the         } *     } $  Panax Notoginseng      while(!que.empty ())//BFS Process -     { thecnt++; +         intsiz=que.size (); A   the          for(intI=0; i<siz; i++)//each point +         { -Node tmp=Que.front (); $              for(intj=0; J<vect[tmp.num].size (); J + +)//the points to be reached $             { -Node q=Vect[tmp.num][j]; -                 if(Q.Y+D&GT;=YY)//has arrived the                 { -printf"yes\n%d\n", cnt+1);Wuyi                     return-1; the                 } -                 Else if(vis[q.num]==false) Wu                 { - Que.push_back (q); Aboutvis[q.num]=1; $Up=max (UP,Q.Y);//the maximum number of points that can be reached -                 } -             } - Que.pop_front (); A         } +     } the     returnUp ; - } $   the   the DoubleDIS (Node A, Node B)//Ask for distance the { the     returnsqrt ((B.Y-A.Y) * (B.Y-A.Y) + (b.x-a.x) * (b.x-a.x)); - } in   the intPre ()//This stone is up to the team, note: Jump up, or jump, do not jump down the { About vect.clear (); thevector< Node >tmp; the      for(intI=0; i<n; i++) the     { + Vect.push_back (TMP); -          for(intj=0; j<n; J + +) the         {Bayi             if(a[i].x==a[j].x && a[i].y==a[j].y) the                 Continue; the             if(A[j].y>=a[i].y && Dis (a[j],a[i]) <=d) -             { - Vect[i].push_back (A[j]); the             } the         } the     } the } -   the   the   the intMain ()94 { the    //freopen ("E://input.txt "," R ", stdin); the     intT; thevector<int>tmp;98Cin>>T; About      while(t--) -     {101memset (Vis,0,sizeof(Vis));102scanf" %d%d%d", &yy, &n, &d);103          for(intI=0; i<n; i++)104         { thea[i].num=i;106scanf"%d%d",&a[i].x,&a[i].y);107         }108  109         if(D&GT;=YY)//one step is up the         {111printf"yes\n1\n"); the             Continue;113         } the   thePre ();//ask each stone to queue the  117         intans=cal ();118         if(ans>-1)119printf"no\n%d\n", yy-ans); -     }121  122     return 0;123}
AC Code

Acdream 1681 long jump Queen (BFS)

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.