Travel distance (15 points)

Source: Internet
Author: User

The buildings of Planet X are all the same and arranged in a matrix style. The number of its buildings is ... When a row is full, from the next line of the adjacent building to the opposite direction to automatic arranging. For example: When the cell automatic arranging width is 6 o'clock, the situation begins as follows:

1 2 3 4 5 6

12 11 10 9 8 7

13 14 15 .....

Our problem is: Known two building numbers m and N, need to require the shortest moving distance between them (can not move diagonally direction)

An integer is required to indicate the shortest moving distance between two floors of M N.

Input format:

Enter in a row as 3 integers w m n, spaces separated, all within the range 1 to 10000 w is automatic arranging width, m,n is the building number to be calculated.

Output format:

An integer is required to indicate the shortest moving distance between two floors of M N.

Input Sample 1:
6 8 2
Output Example 1:
4
Input Sample 2:
4 7 20
Output Example 2:
5

Calculate the vertical distance first, then the horizontal distance.
Code:
#include <iostream>#include<algorithm>#include<map>#include<cstring>#include<cstdio>using namespacestd;intMain () {intW,m,n; scanf ("%d%d%d",&w,&m,&N); M--, N--; intMM = M/W,NN = N/W; intD = ABS (NN-mm); M%=W; N%=W; if(mm%2) m = W-1-m; if(nn%2) n = w-1-N;//cout<<m<< ' <<n<< ';D + = ABS (N-m); cout<<D;}

Travel distance (15 points)

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.