Moving distance
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)
Input is 3 integers w m n, spaces separated, all within 1 to 10000 range
W is automatic arranging width, m,n is the building number to be calculated.
An integer is required to indicate the shortest moving distance between two floors of M N.
For example:
User input:
6 8 2
Then, the program should output:
4
Again for example:
User input:
4 7 20
Then, the program should output:
5
Resource contract:
Peak memory Consumption < 256M
CPU Consumption < 1000ms
Please strictly according to the requirements of the output, do not use the superfluous printing similar: "Please enter ..." Redundant content.
All the code is placed in the same source file, after debugging passed, the copy is submitted to the source.
Note: The main function needs to return 0
Note: Use only ANSI c/ansi C + + standards, and do not invoke special functions that depend on the compilation environment or operating system.
Note: All dependent functions must explicitly #include <xxx> in the source file, and the common header files cannot be omitted from the project settings.
When committing, be careful to choose the type of compiler you expect.
#include <iostream>
#include <cmath>
using namespace Std;
int main ()
{
int w,m,n;
CIN>>W>>M>>N;//W is automatic arranging width, m,n is the building number to be calculated
m--;
n--;
int m1=m/w, m2=m%w;
if (m1&1)
{
m2=w-1-m2;
}
int n1=n/w, n2=n%w;
if (n1&1)
{
N2=W-1-N2;
}
Cout<<abs (M1-N1) +abs (M2-N2) <<endl;
return 0;
}
/*
M1&1 and n1&1 are judged by odd even numbers,
Final output ABS (M1-N1) is this okay? is the number of rows of two buildings, such as 7 and 20,
This is part 3.
Part of ABS (M2-N2) is the way to judge the two floors in the same row.
If M1 or N1 is odd, w-1-m1 indicates the distance from the far left of the building.
That is, the absolute value of the difference between the last two buildings and the left.
At first, m--and n--said that the entire matrix was changed to start from 0.
For example 4 7 20
Matrix:
0 1 2 3
7 6 5 4
8 9 10 11
15 14 13 12
16 17 18 19
20
This is the distance between 6 and 19.
In fact, if you do not do m--and n--change the algorithm is also possible * *
#include <iostream>
#include <cmath>
using namespace Std;
int main ()
{
int w,m,n;
CIN>>W>>M>>N;//W is automatic arranging width, m,n is the building number to be calculated
m--;//initially, m--and n--, said the whole matrix changed to start from 0.
n--;//initially, m--and n--, said the whole matrix changed to start from 0.
int m1=m/w, m2=m%w;
if (m1&1)//If M1 or N1 is odd, w-1-m2 indicates the distance from the far left of the building
{
m2=w-1-m2;
}
int n1=n/w, n2=n%w;
if (n1&1)//If M1 or N1 is odd, w-1-m1 represents the distance from the far left of the building,
Finally, the absolute value of the difference between the two floor and the left.
{
N2=W-1-N2;
}
Cout<<abs (M1-N1) +abs (M2-N2) <<endl;
ABS (M1-N1) Two floor difference line number
ABS (M2-N2) determine the road the two floors need to walk on the same line
return 0;
}
Blue Bridge Cup-moving distance