TheNumberGameDiv2
SRM 574 · 2012-12-13 · by gojira_tc
Problem Statement
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
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.
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.
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.
334 12 Returns: -1
There is no way to obtain 12 from 334.
218181918 9181 Returns: 6
9798147 79817 Returns: -1
Submissions are judged against all 148 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class TheNumberGameDiv2 with a public method int minimumMoves(int A, int B) · 148 test cases · 2 s / 256 MB per case