Problem Statement
You are placed at one end of a field represented by a row of cells. Each cell of the field can be one of the following:
You are given a String field, where each character represents a cell in the field, from left to right. You are also given ints K and C. Return the minimal number of seconds which will bring you to the rightmost cell of the field. If you can't get there, return -1.
- empty: is always passable (represented by a '.');
- contains a kind creature (represented by a 'K'): is passable, unless the creature is in a bad mood. This happens if the number of the second at which you enter it is divisible by K;
- contains a cruel creature (represented by a 'C'): is impassable, unless the creature in in a good mood. This happens if the number of the second at which you enter it is divisible by C.
You are given a String field, where each character represents a cell in the field, from left to right. You are also given ints K and C. Return the minimal number of seconds which will bring you to the rightmost cell of the field. If you can't get there, return -1.
Definition
- Class:
- KindAndCruel
- Method:
- crossTheField
- Parameters:
- String, int, int
- Returns:
- int
- Method signature:
- int crossTheField(String field, int K, int C)
- (be sure your method is public)
Constraints
- field will contain between 2 and 50 characters, inclusive.
- Each character in field will be '.', 'K' or 'C'.
- The first and the last characters of field will be '.'.
- K and C will be between 1 and 50, inclusive.
Examples
"..."
2
5
Returns: 2
There are no creatures on the field, so you just go two cells to the right in two seconds.".K.C."
3
4
Returns: 5
At second 1, you move to the cell with the kind creature. At second 2, you move right to the empty cell. At second 3, you wait in the empty cell, because you can't enter the cell with the cruel creature yet. At second 4, the cruel creature is in a good mood and you can enter its cell. Finally, at second 5 you enter the rightmost cell.".CCCC."
3
5
Returns: -1
You can not pass.".CKKKKCKKKKC.KKKKCKKKKC.CKKKKCKKKKCKKKKCKKKKCKKKK."
5
5
Returns: 60