Topic:
Out of the sky Pie
Time limit: c/C + + language 1000MS; other languages 3000MS
Memory limit: 131072KB for C + + language, other languages 655360KB
Title Description:
We all know that "the sky will not drop pie" this sentence, but one day, Xiao Ming on the way back to school, the sky really fell out of pie. Xiao Ming's character is a bit good, this pie will fall in Xiao Ming's side 10 meters in the range. The pie fell on the ground and obviously could not eat, so xiaoming immediately picked up his backpack to pick up. However, Xiao Ming is a technical house, sports is not very good, every second only in the move not more than 1 meters in the range to catch the pie. The map of this path is as follows:
Now, let's simplify the question, and the pie is now only in the 11 positions of the 0~10. At the beginning, Xiaoming stood in the 5 position, so in the first second he could only receive the pies in one of the three positions of the 4,5,6. Q How many pies can bob pick up? Please use one of the JAVA/PYTHON/C++/C languages to program.
Input
Test data groups, each set of test data will give a total of the number of pies dropped N, followed by the N line gives each pie drop coordinates x and drop time t. May drop multiple pies in the same second. (0<n,t<100000)
Output
For each test data, output an integer representing the maximum number of pies that xiaoming can receive.
Sample input
6
5 1
4 1
6 1
9 |
9 |
8 3
Sample output
Case #1:
The max number of pies is:4
Idea: Time starts from 0 until no pie drops, traversing all possible moves, and the number of pies obtained per way, whichever is greater.
Code (not tested on the test platform):
1 ImportJava.util.*;2 3 Public classmain{4 Private voidFuncintEndTime,intCurtime,intMingplace, Map<integer, arraylist<integer>>Timeplacemap,5 intCurnum, arraylist<integer>numlist) {6 if(Curtime >endTime) {7 Numlist.add (curnum);8 return;9 }Ten One //calculate the number of pies that can be obtained by moving -1,0,+1 steps A intNum_here = 0, Num_before = 0, Num_behind = 0; - if(Timeplacemap.containskey (curtime+1)) { -arraylist<integer> places = Timeplacemap.get (Curtime + 1); theSystem.out.println ("Places:" +places); - for(intplace:places) { - if(Place = =mingplace) { -num_here++; +}Else if(Mingplace > 0 && Place = = MingPlace-1) { -num_before++; +}Else if(Mingplace < && place = = Mingplace + 1) { Anum_behind++; at } - } - } - -Func (EndTime, curtime+1, Mingplace, Timeplacemap, -Curnum + Num_here, numlist);//The position does not change in if(Mingplace > 0) { -Func (EndTime, curtime+1, mingPlace-1, Timeplacemap, toCurnum + Num_before, numlist);//position minus 1 + } - if(Mingplace < 10) { theFunc (EndTime, curtime+1, mingplace+1, Timeplacemap, *Curnum + Num_behind, numlist);//location plus 1 $ }Panax Notoginseng } - the Public Static voidMain (string[] args) { +Main MainClass =NewMain (); AScanner in =NewScanner (system.in); the while(In.hasnext ()) { +String input =In.next (); - intn =integer.parseint (input); $Map<integer, arraylist<integer>> timeplacemap =NewHashmap<>(); $ for(inti=0; i<n; i++) { - intPlace =Integer.parseint (In.next ()); - intTime =Integer.parseint (In.next ()); thearraylist<integer> places = Timeplacemap.getordefault (Time,NewArraylist<integer>()); - Places.add (place);Wuyi timeplacemap.put (time, places); the } - intEndTime =Collections.max (Timeplacemap.keyset ()); WuArraylist<integer> numlist =NewArraylist<>(); -Mainclass.func (endTime, 0, 5, timeplacemap, 0, numlist); About intMaxnum =Collections.max (numlist); $System.out.println ("The max number of pies is:" +maxnum); - } - in.close (); - } A}
Pie in the sky--Mobile Research institute 2018 school recruit Pen question