Statistics

Problem Statement for "BishopMove"

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.

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

  1. 4

    6

    7

    3

    Returns: 1

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

  2. 2

    5

    2

    5

    Returns: 0

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

  3. 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. 4

    6

    7

    4

    Returns: -1

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

  5. 2

    1

    7

    6

    Returns: 1

  6. 1

    2

    1

    4

    Returns: 2

  7. 7

    0

    1

    6

    Returns: 1

  8. 0

    0

    1

    6

    Returns: -1

  9. 0

    7

    2

    5

    Returns: 1

  10. 7

    0

    2

    7

    Returns: 2

  11. 3

    3

    2

    6

    Returns: 2

  12. 0

    3

    1

    5

    Returns: -1

  13. 5

    1

    4

    0

    Returns: 1

  14. 0

    0

    0

    6

    Returns: 2

  15. 2

    6

    5

    3

    Returns: 1

  16. 1

    3

    1

    5

    Returns: 2

  17. 1

    1

    1

    1

    Returns: 0

  18. 1

    2

    1

    2

    Returns: 0

  19. 0

    0

    1

    1

    Returns: 1

  20. 1

    1

    0

    2

    Returns: 1

  21. 7

    6

    4

    3

    Returns: 1

  22. 1

    1

    1

    4

    Returns: -1

  23. 2

    6

    1

    3

    Returns: 2

  24. 1

    3

    5

    3

    Returns: 2

  25. 0

    1

    0

    3

    Returns: 2

  26. 5

    4

    6

    5

    Returns: 1

  27. 6

    0

    0

    0

    Returns: 2

  28. 1

    1

    2

    2

    Returns: 1

  29. 1

    7

    2

    5

    Returns: -1

  30. 1

    1

    1

    7

    Returns: 2

  31. 0

    0

    6

    6

    Returns: 1

  32. 0

    2

    4

    1

    Returns: -1

  33. 0

    0

    3

    1

    Returns: 2

  34. 0

    0

    2

    0

    Returns: 2

  35. 3

    5

    4

    7

    Returns: -1

  36. 1

    2

    0

    5

    Returns: 2

  37. 1

    1

    7

    3

    Returns: 2

  38. 2

    1

    1

    2

    Returns: 1

  39. 2

    2

    3

    4

    Returns: -1

  40. 6

    6

    1

    6

    Returns: -1

  41. 3

    4

    5

    6

    Returns: 1

  42. 1

    2

    4

    3

    Returns: 2

  43. 2

    2

    2

    2

    Returns: 0

  44. 1

    2

    3

    2

    Returns: 2

  45. 7

    7

    0

    0

    Returns: 1

  46. 3

    5

    7

    3

    Returns: 2

  47. 2

    2

    4

    2

    Returns: 2

  48. 0

    0

    7

    7

    Returns: 1

  49. 0

    0

    0

    1

    Returns: -1

  50. 1

    4

    4

    5

    Returns: 2

  51. 0

    2

    2

    2

    Returns: 2

  52. 1

    2

    7

    6

    Returns: 2

  53. 0

    0

    0

    2

    Returns: 2

  54. 3

    1

    0

    2

    Returns: 2

  55. 2

    1

    3

    2

    Returns: 1

  56. 3

    3

    4

    3

    Returns: -1

  57. 3

    1

    1

    1

    Returns: 2

  58. 0

    0

    4

    6

    Returns: 2

  59. 2

    1

    5

    6

    Returns: 2

  60. 1

    1

    7

    7

    Returns: 1

  61. 2

    5

    3

    2

    Returns: 2

  62. 0

    1

    0

    7

    Returns: 2


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: