The sword refers to offer--NetEase written test to save the small easy

Source: Internet
Author: User

Key points of knowledge

First introduce Manhattan, Manhattan is a very busy neighborhood, tall buildings, streets, from a location to the B location no straight path, must bypass, and at least by the C location, walk AC and CB to reach, because the streets are very regular, ACB like a right angle 3 angle, AB is the hypotenuse, AC and CB are right-angled side, according to Pythagoras (Hook) theorem, or vector theory, can be known with AC and CB can express the length of AB.

In the early computer graphics, the screen is composed of pixels, is an integer, the coordinates of the point is generally an integer, because the floating-point operation is very expensive, very slow and error, if the direct use of the distance AB, you have to do floating-point operations, if the use of AC and CB, then as long as the calculation plus and minus, which greatly , and no matter how many times the cumulative operation, there is no error. As a result, computer graphics uses Manhattan to name this representation.

Manhattan distance: Two points in the north-south direction distance plus the east-west upward distance, namely D (i,j) =|xi-xj|+|yi-yj|. For a town street with a north-south, east-east-west-to-regular layout, the distance from one o'clock to another point is precisely the distance traveled in the north and west, plus the distance between eastern and Western travel.

By analyzing the following topics, it is possible to apply the nearest point of the Manhattan distance calculation to the (at) point, and calculate the result value based on the Manhattan distance.

  The code is as follows :

1 /**2 * Save Small Easy3 There is a piece of 1000*1000 grass, small easy to initially stand in (the upper left corner of the position). Small easy in every second will be moved horizontally or vertically to the adjacent grass grazing (small easy will not go out of the border). 4 Big villain Super Super want to catch cute small easy, he has n traps in his hand. The first trap is placed in the horizontal axis of Xi, the ordinate is Yi's position, the small easy once walked into a trap, will be ultra-super capture. 5 you want to go to save small easy, need to know small easy at least how many seconds may go into a trap, so early rescue small easy. 6 Input Description:7 The first behavior is an integer n (n≤1000), which indicates that the hyper-super has a total of n traps. 8 the second line has n integer xi, which indicates the horizontal axis of the first trap9 The third line has n integer yi, which indicates the ordinate of the first trapTen ensure that the coordinates are within the grassland range.  One  A Output Description: - output An integer that indicates that the smallest possible number of seconds to fall into super-super traps -  the Input Example: - 3 - 4 6 8 - 1 2 1 + Output Example: - 3 + Ideas: A calculate the shortest distance at */ -  -#include <iostream> -#include <vector> - using namespacestd; -  in intMainvoid){ -     intTrapnum; to  +      while(Cin >>trapnum) { -         if(Trapnum <=0|| Trapnum > +) the             Continue; * //vector<int> DX; $ //vector<int> dy;Panax Notoginsengvector<int> dx (trapnum);//Specifies the container size, otherwise it overflows -vector<int>dy (trapnum); the          for(inti =0; i < Trapnum; i++) +CIN >>Dx[i]; A          for(inti =0; i < Trapnum; i++) theCIN >>Dy[i]; +  -         intresult =99999; $         //enumerate over to maintain minimum value $          for(inti =0; i < Trapnum; i++){ -             intLength = (Dx[i]-1) + (Dy[i]-1); -             if(Length <result) theresult =length; -         }Wuyicout << Result <<Endl; the     } -     return 0; Wu}

The sword refers to offer--NetEase written test to save the small easy

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.