B - Nana in Wonderland Series-long jump queen Time Limit: 2000/1000ms (java/others) Memory Limit: 128000/64000kb (java/others) 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: Give a picture, start point, end point, ask whether it can reach the end, if it can, output a minimum number of steps, if not, output distance to the nearest point of the end
Solution: Composition, because there are only 1000 points, you can consider the adjacency matrix, two points distance is less than or equal to a side of D, Y<=d Point and the beginning of a side (meaning can arrive from the beginning), y<=y0-d Point and the end of a side (meaning can reach the end), don't forget y0< =d to the beginning and end of a side (or a special output Yes 1) and then run on the map BFS or the rest of the shortest path algorithm, how to record the last distance can not reach? When traversing to a point, it is possible to update mindist=min (MINDIST,Y0-Y) by determining the size of the y-coordinate and the y0. If the end can not be reached, output mindist can be.
Note: The shortest way to forget how to write, and then to learn, directly with the search. Originally with Dfs time-out, and then to the amount, should be a wide search, ac ...
1. First determine whether the distance jumps d>=y0, if set up direct output results.
2. If not, the first stone that can be connected to the starting point is labeled 1 with the array mark.
3. Compare the stone with the previously imported stone and connect it with a two-dimensional array map.
4. After the search is from 1 to find the stones to meet the current number of steps, and then find the stones can be connected and the next stone step must be less than the current number of steps no+1, in order to put the next stone mark, if not find the successor to the end. During each find step, save the nearest distance dis.
5. Cycle through the stones (Y0-y[i]<=d) that can jump to the end, find the number of steps that are not 0 and the smallest step is the result step. If there is no stone to meet, will not jump to the end, the nearest distance is dis.
1#include <stdio.h>2#include <string.h>3 4 inty0,n,d,map[1001][1001],x[1001],y[1001],mark[1001],way,sum,der;5 6 intGetdis (intIintj)//ask for the square of two points to compare with the jumping distance d*d7 {8 return(X[j]-x[i]) * (X[j]-x[i]) + (Y[j]-y[i]) * (y[j]-y[i]);9 }Ten One voidGointNO)//starting from the number of steps no=1 to find A { - //printf ("-%d\n", no); - inti,j,k=0; the for(i=0; i<n;i++) - { - if(mark[i]==NO)//whether the current number of steps is satisfied - { + for(j=0; j<n;j++) - { + if(map[i][j]==1&& (mark[j]==0|| mark[j]>no+1) //Find the next stone to jump to, and the number of steps to jump is 0 or greater than the current number of steps no+1 A { at if(der>y0-y[j])//Determine the closest distance from the end point -der=y0-Y[j]; -mark[j]=no+1; -k=1; - } - } in } - } to if(k)//presence of the next pebble can jump +Go (no+1); - } the * intMain () $ {Panax Notoginseng intt,i,j; - while(SCANF ("%d", &t)! =EOF) the { + while(t--) A { thescanf" %d%d%d",&y0,&n,&d); +way=0; -sum=1003; $Der=y0; $ if(y0<=d)//end point is closer than jumping distance - { -sum=1; theway=1; - }Wuyimemset (Mark,0,sizeof(Mark)); thememset (Map,0,sizeof(map)); - for(i=0; i<n;i++) Wu { -scanf"%d%d",&x[i],&y[i]); About if(y[i]<=d)//The Stones to which the starting point can jump $ { - if(der>y0-Y[i]) -der=y0-Y[i]; -mark[i]=1; Mark the number of steps as 1 A } + for(j=0; j<i;j++) the { - if(Getdis (I,J) <=d*d)//two pebble distance is less than or equal to D $ { themap[i][j]=1; themap[j][i]=1; the } the } - } in if(!) the { theGo1); About for(i=0; i<n;i++) the { the if(y0-y[i]<=d&&mark[i]!=0)//There is a pebble (mark[i]!=0) that can be jumped to, and the stone can jump to the end the { +way=1; - if(sum>mark[i]+1) thesum=mark[i]+1;Bayi } the } the } - if(To) -printf"yes\n%d\n", sum); the Else theprintf"no\n%d\n", der); the } the } - return 0; the}
PS: Play their own dizzy @#@, in this thank Lin Class head ^_^ o~ efforts!
B-Nana in Wonderland Series-long jump Queen