TopcoderArchiveVisit Topcoder
SRM13 Dec 2012
SRM 574

TheNumberGameDiv2

Problem statement, definition, constraints, and public examples.

Problem Statement

Manao has recently invented a brand new single-player game called The Number Game.

The player starts with a number A. Also, another number B is chosen. Note that neither A nor B contain a zero digit in their base 10 representation.

The player's goal is to obtain B from A. In each move, the player can either reverse his current number, or he can divide it by 10 (using integer division). For example, if the current number is 12849, the player can either reverse it to obtain 94821, or he can divide it by 10 to obtain 1284. (Note that we always round down when using integer division.)

You are given two ints A and B. If it is possible to obtain B from A, return the minimum number of moves necessary to reach this goal. Otherwise, return -1.

Definition

Class:
TheNumberGameDiv2
Method:
minimumMoves
Parameters:
int, int
Returns:
int
Method signature:
int minimumMoves(int A, int B)
(be sure your method is public)

Constraints

  • A will be between 1 and 999,999,999, inclusive.
  • B will be between 1 and 999,999,999, inclusive.
  • A and B will not contain a zero digit in base 10 representation.
  • A and B will be distinct.

Examples

  1. 25
    5
    Returns: 2
    Initially, the player has number 25 and needs to obtain 5. He can reverse the number and obtain 52, then divide it by 10 and obtain 5.
  2. 5162
    16
    Returns: 4
    To obtain 16 from 5162 in four moves, the player can perform the following sequence of moves: Reverse the number and obtain 2615. Divide 2615 by 10 and obtain 261. Reverse 261 and obtain 162. Divide 162 by 10 and obtain 16. Note that this is not the only possible sequence of four moves which leads to the goal.
  3. 334
    12
    Returns: -1
    There is no way to obtain 12 from 334.
  4. 218181918
    9181
    Returns: 6
  5. 9798147
    79817
    Returns: -1
Back to all problems