Connection Status:
Competition Arena > BishopMove
SRM 628 · 2014-06-16 · by w10d · Simple Math
Class Name: BishopMove
Return Type: int
Method Name: howManyMoves
Arg Types: (int, int, int, int)
Problem Statement

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.
(Of course, the bishop's destination must always be a valid cell on the chessboard.)

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 ints r1, c1, r2, and c2. Compute and return the smallest number of moves a bishop needs to get from (r1,c1) to (r2,c2). If it is impossible for a bishop to reach the target cell, return -1 instead.

Constraints

  • r1,c1,r2,c2 will be between 0 and 7, inclusive.
Examples
0)
4
6
7
3
Returns: 1

The bishop can go from (4,6) to (7,3) in a single move.

1)
2
5
2
5
Returns: 0

The bishop is already where it should be, no moves are necessary.

2)
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.

3)
4
6
7
4
Returns: -1

If the bishop starts at (4,6), it can never reach (7,4).

4)
2
1
7
6
Returns: 1

Submissions are judged against all 62 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class BishopMove with a public method int howManyMoves(int r1, int c1, int r2, int c2) · 62 test cases · 2 s / 256 MB per case

Submitting as anonymous