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)