Problem Statement
Janusz is learning how to play chess. He is using the standard chessboard with 8 rows and 8 columns. Both the rows and the columns are numbered 0 through 7. Thus, we can describe each cell using its two coordinates: (row, column).
Janusz recently learned about one of the chess pieces: the bishop. The bishop is a piece that moves diagonally by an arbitrary number of cells. Formally, if a bishop is currently on the cell (r,c) of an empty chessboard, the set of all cells reachable in a single move contains the following cells:
- All cells of the form (r+k,c+k), where k is a positive integer.
- All cells of the form (r+k,c-k), where k is a positive integer.
- All cells of the form (r-k,c+k), where k is a positive integer.
- All cells of the form (r-k,c-k), where k is a positive integer.
Janusz took an empty chessboard and he placed a single bishop onto the cell (r1,c1). He now wants to move it to the cell (r2,c2) using as few moves as possible.
You are given the
Definition
- Class:
- BishopMove
- Method:
- howManyMoves
- Parameters:
- int, int, int, int
- Returns:
- int
- Method signature:
- int howManyMoves(int r1, int c1, int r2, int c2)
- (be sure your method is public)
Constraints
- r1,c1,r2,c2 will be between 0 and 7, inclusive.
Examples
4
6
7
3
Returns: 1
The bishop can go from (4,6) to (7,3) in a single move.
2
5
2
5
Returns: 0
The bishop is already where it should be, no moves are necessary.
1
3
5
5
Returns: 2
In the first move Janusz can move the bishop to the cell (4,6). Please note that this is the largest possible return value: whenever there is a solution, there is a solution that uses at most two moves.
4
6
7
4
Returns: -1
If the bishop starts at (4,6), it can never reach (7,4).
2
1
7
6
Returns: 1
1
2
1
4
Returns: 2
7
0
1
6
Returns: 1
0
0
1
6
Returns: -1
0
7
2
5
Returns: 1
7
0
2
7
Returns: 2
3
3
2
6
Returns: 2
0
3
1
5
Returns: -1
5
1
4
0
Returns: 1
0
0
0
6
Returns: 2
2
6
5
3
Returns: 1
1
3
1
5
Returns: 2
1
1
1
1
Returns: 0
1
2
1
2
Returns: 0
0
0
1
1
Returns: 1
1
1
0
2
Returns: 1
7
6
4
3
Returns: 1
1
1
1
4
Returns: -1
2
6
1
3
Returns: 2
1
3
5
3
Returns: 2
0
1
0
3
Returns: 2
5
4
6
5
Returns: 1
6
0
0
0
Returns: 2
1
1
2
2
Returns: 1
1
7
2
5
Returns: -1
1
1
1
7
Returns: 2
0
0
6
6
Returns: 1
0
2
4
1
Returns: -1
0
0
3
1
Returns: 2
0
0
2
0
Returns: 2
3
5
4
7
Returns: -1
1
2
0
5
Returns: 2
1
1
7
3
Returns: 2
2
1
1
2
Returns: 1
2
2
3
4
Returns: -1
6
6
1
6
Returns: -1
3
4
5
6
Returns: 1
1
2
4
3
Returns: 2
2
2
2
2
Returns: 0
1
2
3
2
Returns: 2
7
7
0
0
Returns: 1
3
5
7
3
Returns: 2
2
2
4
2
Returns: 2
0
0
7
7
Returns: 1
0
0
0
1
Returns: -1
1
4
4
5
Returns: 2
0
2
2
2
Returns: 2
1
2
7
6
Returns: 2
0
0
0
2
Returns: 2
3
1
0
2
Returns: 2
2
1
3
2
Returns: 1
3
3
4
3
Returns: -1
3
1
1
1
Returns: 2
0
0
4
6
Returns: 2
2
1
5
6
Returns: 2
1
1
7
7
Returns: 1
2
5
3
2
Returns: 2
0
1
0
7
Returns: 2