PROBLEM STATEMENT
We are going to use a new measurement system that uses knight moves to
determine distance. The distance between 2 spots on the board will be the
smallest number of moves required by a knight to get from one to the other. A
knight moves as follows:
(1) It first takes two steps in the same direction either up, down, left, or
right
(2) It then takes one step perpendicular to the movement it made in (1) (e.g.
if it went up it could go left or right)
(3) The knight is never allowed to jump off of the board
0123456
6.......6
5..D.D..5
4.D...D.4
3...K...3 Y
2.D...D.2
1..D.D..1
0.......0
0123456
X
The knight K(at 3,3) can move to any of the D spots shown above. The numbers
show the coordinate system of the board. In chess, coordinates always begin in
the lower lefthand corner of the board growing upward and to the right. Given
two spots on the board you will return the distance between them in knight
moves. If the two given spots are the same spot then no moves are required so
return 0.
Create a class KnightMove that contains the method distance, which takes an int
boardSize, an int[] x, and an int[] y, and returns an int representing the
distance in knight moves between the two given spots.
DEFINITION
Class: KnightMove
Method: distance
Parameters: int, int[], int[]
Returns: int
Method signature (be sure your method is public): int distance(int boardSize,
int[] x, int[] y);
Note
- x[0] and y[0] are the x-coordinate and y-coordinate respectively of the first
spot on the board
- x[1] and y[1] are the x-coordinate and y-coordinate respectively of the
second spot on the board
- x-coordinates are measured from the leftmost column(file) and are 0-based
- y-coordinates are measured from the bottommost row(rank) and are 0-based
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- boardSize will be between 4 and 100 inclusive
- Each element of x will be between 0 and boardSize-1 inclusive
- Each element of y will be between 0 and boardSize-1 inclusive
- x will contain exactly 2 elements
- y will contain exactly 2 elements
EXAMPLES
In the following examples 'A' will denote one spot whereas 'B' will denote the
other spot
'.' will be used for spots on the board
Numbers will denote the steps in the path taken by the piece denoted by 'A'
1) boardSize = 8
x = {0,7}
y = {0,7}
The board looks like: One possible shortest path looks like:
.......B .......6
........ .....5..
........ ........
........ ....4...
........ ...2....
........ .....3..
........ ..1.....
A....... 0.......
All shortest paths between 'A' and 'B' require 6 moves.
Method returns 6.
2) boardSize = 5
x = {0,0}
y = {0,0}
Both given spots are the same so no moves are required.
Method returns 0.
3) boardSize = 6
x = {0,1}
y = {0,0}
The board looks like: One possible shortest path looks like:
...... ......
...... ......
...... ......
...... 2.....
...... ..1...
AB.... 03....
All shortest paths between 'A' and 'B' require 3 moves.
Method returns 3.
4) boardSize = 100
x = {0,99}
y = {0,99}
Method returns 66.
5) boardSize = 100
x = {0,99}
y = {0,0}
Method returns 51.