"Programming Marathon" "026-is the man on the next 100 floor"

Source: Internet
Author: User
Tags cos diff

"Programming Marathon algorithm Directory" "026-is the man on the next 100 Floor" "Engineering download >>>" 1 Topic Description

I believe everyone has heard that "is a man on the next 100 layers" series of games, including multiple lengths and heights of different platforms, the ground is the lowest platform, height is zero, length unlimited.
A man begins to fall from somewhere above all the platforms, and its falling speed is always 1 m/s. When he landed on a platform, the player chose to let him run to the left or right, running at a speed of 1 m/s. He will continue to fall when he runs to the edge of the platform. Requires that the height of each drop cannot exceed max meters, otherwise it will fall dead and the game will end.
Please help design a program to calculate the fastest time to reach the ground.

1.1 Input Description:

The input contains multiple sets of data.
The first line of each set of test data is four integers n, X, Y, MAX, separated by a space. n is the number of platforms (excluding the ground), X and y are the coordinates of the man's position at the beginning of the game, and Max is the maximum height of the drop.
followed by n rows, each line describes a platform's information, including three integers, x1[i],x2[i] and h[i]. H[i] Indicates the height of the platform, X1[i] and x2[i] represent the horizontal axis of the platform's left and right endpoints.
1≤n≤1000;-20000≤x, X1[i], X2[i]≤20000;1≤h[i] < y≤20000 (1≤i≤n). The units of all coordinates are meters.
The thickness of the platform is negligible, and if it falls on the edge of a platform, it is considered to be on the platform. All platforms are non-overlapping or connected. There must be a solution to the test data guarantee problem.

1.2 Output Description:

Corresponds to each set of inputs, outputting an integer that is the earliest time to reach the ground.

1.3 Input Example:
3 8 17 200 10 80 10 134 14 3
1.4 Output Example:
23
2 ideas for solving problems



  Fig. 1 Bottom 100-layer shortest walk method
Think of the starting point as a layer with a width of 1, which is handled uniformly with the other layers. The minimum distance from any layer (any layer above the second level from bottom to bottom) to the lowest level.
1) jump directly from the starting point to the bottom.
2) from the starting point, jump to the next layer, and then go from the left edge to the bottom.
3) from the starting point, jump to the next layer, then from the right edge to the bottom.
The minimum distance from the above three cases is the minimum distance from a layer to the ground. Initialize the minimum distance of the first layer drop, and then push it up to the jump point to get the answer.
  

3 Algorithm Implementation
ImportJava.util.Arrays;ImportJava.util.Comparator;ImportJava.util.Scanner;/** * Author: Wang Junshu * time:2016-05-13 19:19 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https://github.com/wang-ju N-chao * declaration:all rights Reserved!!! */ Public  class Main {    //Coordinate class    Private Static  class coordinate {        //Horizontal axis left end        intA//Horizontal Right end        intb//Ordinate        intY Coordinate (intAintBintY) { This. A = A; This. B = b; This. y = y; }    } Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in); while(Scanner.hasnext ()) {intn = scanner.nextint ();intx = Scanner.nextint ();inty = Scanner.nextint ();intmax = Scanner.nextint (); coordinate[] cos =NewCoordinate[n +1]; for(inti =0; I < n; i++) {Cos[i] =NewCoordinate (Scanner.nextint (), Scanner.nextint (), Scanner.nextint ()); }//Last person's coordinates as a 0-length bezelCos[n] =NewCoordinate (x, x, y);        System.out.println (earliesttime (COS, max));    } scanner.close (); }/** * Minimum time for next 100 layers * * @param cos Platform array * @param max person can drop the maximum height at a time * @return  The shortest time * /    Private Static int EarliestTime(coordinate[] Cos,intMax) {//Sort by height from ground level from small to largeArrays.sort (COS,NewComparator<coordinate> () {@Override             Public int Compare(coordinate o1, coordinate O2) {returnO1.Y-O2.Y; }        });intlen = cos.length;//Layer I to the lowest time, 0 left down minimum time, right down to the minimum time        int[] height =New int[Len] [2];//First bezel from left to right to the bottom of the timeheight[0][0] = cos[0].Y; height[0][1] = cos[0].Y;//From the bottom up to handle other bezel         for(inti =1; i < Len; i++) {//Assume the maximum height of the layer to the next layerheight[i][0] = Integer.max_value; height[i][1] = Integer.max_value;//Whether this layer has been calculated from the left and right down to the next level            Booleanleft =false;Booleanright =false; for(intj = i-1; J >=0; j--) {//Two-layer height difference                intdiff = cos[i].y-cos[j].y;//Height difference satisfies the condition, and the left and right sides do not all calculate well, continue processing                if(diff <= Max && (!left | |!right)) {//Go down from the left of layer I                    if(!left && between (cos[i].a, COS[J].A, cos[j].b)) {//J-Layer "left" side up to the bottom                        if(height[j][0] < Integer.max_value) {//From the "left" side of layer I, and then go down through the "left" side of Layer J.height[i][0] = Math.min ((cos[i].a-cos[j].a) + diff + height[j][0], height[i][0]);//Layer I left down to the next layer has been processedleft =true; }//J-Layer "right" side up to the bottom                        if(height[j][1] < Integer.max_value) {//From the "left" side of layer I, and then go down through the "right" side of Layer J.height[i][0] = Math.min ((cos[j].b-cos[i].a) + diff + height[j][1], height[i][0]);//Layer I left down to the next layer has been processedleft =true; }                    }//Go down from the right side of layer I                    if(!right && between (cos[i].b, COS[J].A, cos[j].b)) {//J-Layer "left" side up to the bottom                        if(height[j][0] < Integer.max_value) {//From the "right" side of layer I, and then go down through the "left" side of Layer J.height[i][1] = Math.min ((cos[i].b-cos[j].a) + diff + height[j][0], height[i][1]);//Layer I right down to the next layer has been processedright =true; }//J-Layer "right" side up to the bottom                        if(height[j][1] < Integer.max_value) {//From the "right" side of layer I, and then go down through the "right" side of Layer J.height[i][1] = Math.min ((cos[j].b-cos[i].b) + diff + height[j][1], height[i][1]);//Layer I right down to the next layer has been processedright =true; }                    }                }Else{//Here to exit the inner for execution, layer I below level J                     Break; }            }//End inner-layer for            //From the left side of the layer I did not encounter the next layer of the bezel, the horizontal axis in the range, and from layer I directly to the 0 layer is accessible,            //i.e. height difference is less than Max            if(!left && checkrange (cos[i].a) && cos[i].y <= max) {height[i][0] = COS[I].Y; }if(!right && checkrange (cos[i].b) && cos[i].y <= max) {height[i][1] = COS[I].Y; }        }returnMath.min (Height[len-1][0], Height[len-1][1]); }/** * Check if the horizontal axis is within range * * @param A-mode coordinate * @return true: In the range, false: no */    Private Static Boolean Checkrange(intA) {returnA >=-20000&& a <=20000; }/** * Check if A is within the [x, y] range * * @param A value * @param x Start value * @param y end Value * @return true: Within range, false: not in range */    Private Static Boolean between(intAintXintY) {returnA >= x && a <= y; }}
4 Test Results

5 Other information

Because Markddow is not good for editing, uploading a picture of a document for reading. PDF and Word documents can be "downloaded >>>" on GitHub.

"Programming Marathon" "026-is the man on the next 100 floor"

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.