Problem Statement
Next to the board, there are 4 buttons labeled "Left", "Right", "Up", and "Down". The game is played by pushing these buttons. When you push a button, all coins will try to move one cell in the corresponding direction. For each coin, there can be three different outcomes:
- If the next cell in the chosen direction contains an obstacle, the coin remains in its current cell.
- If there is no next cell in the chosen direction (i.e., if the coin is already on the corresponding edge of the board), the coin falls off the board.
- In all other cases, the coin moves one cell in the chosen direction. Note that this includes the case when the destination cell currently contains another coin.
The goal of the game is to make exactly one coin fall off the board. If that is impossible, or if you need to push more than 10 buttons in order to achieve the goal, return -1. Otherwise, return the smallest number of times you need to push a button in order to achieve the goal.
Definition
- Class:
- CoinsGameEasy
- Method:
- minimalSteps
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int minimalSteps(String[] board)
- (be sure your method is public)
Notes
- There may be some sequences of moves that will cause both coins to end in the same cell. Note that when trying to win the game such moves should be avoided.
Constraints
- board will contain between 1 and 20 elements, inclusive.
- Every element of board will have the same length, and this length will be between 1 and 20, inclusive.
- Each character in each element of board will be one of '#', 'o' and '.'.
- There will be exactly 2 'o' in board.
Examples
{"oo"}
Returns: 1
Push the Left button. The left coin will fall, the right one will move one step left and it will still be on the board.
{".#", ".#", ".#", "o#", "o#", "##"}
Returns: 4
You need to push the Up button 4 times in a row.
{"..", "..", "..", "o#", "o#", "##"}
Returns: 3
As in Example 1, this game can be won by pushing the Up button 4 times. However, there is also a shorter solution: push Up, Right, and Right, in this order.
{"###", ".o.", "###", ".o.", "###"}
Returns: -1
{"###", ".o.", "#.#", ".o.", "###"}
Returns: 3
{"###########", "........#o#", "###########", ".........o#", "###########"}
Returns: 10
{"############", ".........#o#", "############", "..........o#", "############"}
Returns: -1
You need at least 11 steps to win the game, so you should return -1.
{"##..#",".#o#.","#.###","..###","#####","#####","#o..#","##...","#####","###..","#####",".#..#","#####","###.#","#####","#.##."}
Returns: 2
{"...#......","..#.......","....#..###","#..#....#.","#.........",".......##.",".......o..","..#...##..","o........."}
Returns: 1
{"##..#",".#o#.","#.###","..###","#####","#####","#o..#","##...","#####","###..","#####",".#..#","#####","###.#","#####","#.##."}
Returns: 2
{"#.....#..####o..###","..##...####.##..##.","##..####..o#.##..#.","..#.##.##..##..#..#","###....#..##..###.#"}
Returns: 1
{".#..o.#.#...#....",".#.###..#o..#...."}
Returns: 1
{"#.....#...#..##...","..#o..#..#......#.","..#..##....#.....#","##...##.#......###",".....#...####..##.","..#....###.##.#...","#.....##..#.##....","...#..#..##.#.....",".#.###..o..#..#..#",".#......#..#.....#"}
Returns: 2
{".#########..","#######.####","############","########..#.","...##.######",".###..#.####","..##########","######o#####","###o####.###","#####.#####.","#######.#.##","##.#####.#.#","####.##.####","######.##.##","###....####.","##.#######.#","############","###.####..#.","######.#.##."}
Returns: -1
{"###","o#o","###"}
Returns: 1
{"....#........","..#o.#.......",".#........##.","....####....#",".......##....","##.....#..#..","#....#..#.##.","....o##.#..#.","..........#.."}
Returns: 2
{".#..#####",".#..o..#.","..#####.#","####.##.#","#.####.##","#.#.#.##.","###.####.",".#.##....","#.#.#.###","..#.####.","##.#.o##.","####..#.#","..#..#.##","##.###.##"}
Returns: 3
{".o#.##","##o.##","######"}
Returns: 1
{"....##.##o..#.##.",".#......######.o#"}
Returns: 1
{"#.#####.####","############","#.#####.####","##########.#","#..##o..#...","####o#.##.##"}
Returns: 1
{"#########.##.###","#.#####.#.######","..####.#####.##.","###.###...###.##","##...#######.###","######..##..####","################","#.#.####.#######","################","###.##.#####.###","##.###..##.#####","#.##.#.##.####.#",".#####.##.######","#..###.##.######","#############oo#",".###########.##."}
Returns: -1
{"..........","..........","..........","..........","..........","..........","..........","......o...","..........","..........","......o...","....#.....",".........."}
Returns: 3
{"#.#.#.#.###","##...#.#.##",".#..#o.#...",".#.##.##..#","#....####.#","####.#..o##",".##...##..."}
Returns: 2
{"..o..............",".................",".o...............","................."}
Returns: 1
{".#.#....#...###.","#..#..###.#oo#..","...#.....#.....#"}
Returns: 2
{"#.","##",".#","##","o.","o.","##",".#",".#",".#","##","##","#.",".#","..","..",".#"}
Returns: -1
{"...o..#.","..##...#",".....#.#","###.##..","..##....","........","..###.##",".#.#.#.#","..#....#","..#...o.",".......#","##.#....","#.#...##"}
Returns: 1
{"..#..##.####","#....#######","########.###","####..######","##.#o#######","#.###.####..","############","#####.######","####.#.#.#.#","############","###.####.##.","#########..#","########.###","#..#####o###"}
Returns: 1
{"....#........",".......o.....","......#......","...##........",".............",".#...........",".#.....#...#.",".............","#............",".........#...","........o..#."}
Returns: 1
{"###.###","###.###","###.###","##..##.","#####.#",".######","#####..","####o##","#####o#","##.####"}
Returns: -1
{"...#.#.","##..o.#","...#...","..o#..."}
Returns: 1
{"###",".##","###","###","###","o##","#o#","###","###"}
Returns: 1
{"#####.#..#######.##",".###.#####..##...#.","##.#.#..####..#####",".##.#...###..##..o#","..##.########.##.##","..######..#..#...##","####..#.#.####.###o","###.#.##.#..###.#.#","######..##.##...###"}
Returns: 1
{"#..##.####",".##oo.#.#.",".#..#..#.#","..#.##.###",".##.#..##.",".###.#.###","#.#.#.....","##..##.###","#.#...#.#.","....###..#",".#.#.##...","..##..###.","....#.#..#",".##..#.#.#","...#..#..#","###.##.#..","###...###."}
Returns: 3
{"..#..o.....",".......o..."}
Returns: 1
{".o..#.....#..#","#...#..o.###.#",".#............",".............."}
Returns: 1
{"#.#.o","#.#..",".####",".#.#.",".#.#.","#..##","...o#","##.#.","...##","#...#",".####","###..","##.##","##..#",".....","##..#"}
Returns: 1
{"#########","#########","########o","o########"}
Returns: 1
{"#########.#########","############o######","######.#o#####.####","####.#.#####.######"}
Returns: -1
{"....#o##.#....","...##...o....#"}
Returns: 1
{"#..##..######.##...",".####.##...##.#...#",".########.#..##...#","##.####......#.##.#","#.#......#..o.#..#.","#.##.#####..##.#.#.","..#.....##.##.#####","#.##.#....#.#..##.#","####.#.#.##.....#..",".#.....##.....#####","..o#..#...#...##.#.","...##..####.#...#.#"}
Returns: 2
{"..#.#...#","..o......","###.....#",".........","..#......","...##....","##..##...","........#",".##...#..","#..##....","#.#..#..#","........#","....##.o.","#..#.....","...#....."}
Returns: 2
{".#.o.#","#.#o.."}
Returns: 1
{"####.##.###","#.###..#.##","####....#..","..#.##o.##.","#..##o#####","#.##.####.#"}
Returns: -1
{"........#........",".................","#........#.....#.","...........o.....","...#........#....","#................","....#............","#......#....o....",".............#...",".......#..#......",".................","...............#."}
Returns: 4
{"#####","##.#o","..###","###.#","#####","#.o##","#####"}
Returns: 1
{"#...#.##..o.##.####","......#.........#.#",".#.##..#..#..#...#.","..######..##.#....#","#...#.#..#....#o..#",".#...#.#..##..#..##","...###..#...#..#.#.","...###..#####.#....","..#..##.#..#..#.###","#.....###..##.....#"}
Returns: 1
{"..........#.......","..................","..................",".............o....","..................","..#........#......","..................","...........#.....#","..................",".#.....o.........#"}
Returns: 1
{".o.#...","#......",".#..o..","......#",".......",".......","..##...",".....##","......."}
Returns: 1
{".#####.","#######","###.##o","#.#####","#.###..","#.#####","#######","#.#####","###.###","o######","##.####",".###.##","...##.#"}
Returns: 1
{".#.###","#####.","#.#...","#.###.",".o.#..","#..###","##.###","###.##",".##.##","###.##","##..##","###o#.","#####.","####.#",".#####"}
Returns: 2
{"#####.##########","#############.##","#######o########","################","################","#####.#########.","################","######.#########","###########.##.#","###########o####","################","################"}
Returns: -1
{".....o#.....","..##..o.#.##",".....#.....#"}
Returns: 1
{"#####","#####","###o#","o####","#####","#####"}
Returns: 1
{".............","...#.....#...","#....##....#o","....###..##..",".#.....#.....","......#...#..","...#.....#..#",".....##......",".####o....##.","##....#....#.","...#..#......","#.#..#.#.#...","#........#...","#.......##..."}
Returns: 1
{"##.###############","##################","############o#####","###############.##","##############.#.#","##################","######.###########","#################.","#..##############.","#######.##.#######","##########.#######","###############.##","###############.##","#########.######o#","########.#########","##################","##################"}
Returns: -1
{"##...#...#..........",".....#.....#.##.##.#",".#.#.....o...#...#.#","#......#.#...#.##...","......##....#.###...",".........#.###...#..",".#...#......#.......",".#####.#........#...",".#..................",".....#......#.......",".#...##..##....#....","#.....#.##.o.#...#..",".##......#......#.#.","#...#..#.#...###....","...#.#......#.#.....","...#.....##......#..","...#...###.....#.#.."}
Returns: 4
{"......#.#","###...#..","#..##...#","##......#","#..#....#","o#..#....","#..#....#",".........","##.##....","#..##...#","##...##..","#.###.#..","......#o."}
Returns: 1
{"......","......",".....#","......","...##.","##....","#..#..","##....","##....","......","......",".o....","..#...","#..#..","...#.#","......","#o..#.",".#....","......","......"}
Returns: 2
{"##..##.##","##.##.###","##..#####",".########","#########","###.o.###","#########","##.######","#o#####..",".##.#.#.#","#..####.#"}
Returns: -1
{".#....","..#.o.","...##.",".....o","......","....#."}
Returns: 1
{"..","..","..","#.",".o","##","#.",".#","..","..","#o"}
Returns: 1
{"##o#.########","#..#.#..#####","o#####.#.#.##"}
Returns: 1
{"###############","####.###..#.###","##########.####","#o#############","###.#########.#","######.########","#############.#","########.###.##","##.############","###.##.#.######","###############","########.###.##","####o##.#######","###############"}
Returns: -1
{"o#.#.#.#","#.#..##.",".##...##","#.####o.","#####...","..#..###","##.#.#.#",".##.###.",".....#.#","...#####",".##.####"}
Returns: 1
{"###..##.#..#....","..####.......o..",".##.###..##.##o#",".#.##..##.#...#.","..#.####..####..","##..##.#.#.###.."}
Returns: 2
{"...","...","o.o",".##"}
Returns: 1
{"#.####..###.###.#.","..##.#..#####o..##","#.#..#####.####..#",".##.#....#####.###","###..#.#..##..####","#.##.#.#..#.#####.","...#...#..##.####.","###.#.######.#####","...##.#.#...#..#..","##.#.####..##.#.##","....#.o...##..###."}
Returns: 1
{".......",".......","..o....",".......",".......",".......",".......",".......","......#",".......",".......",".......",".......",".......","..o....",".......",".......",".......","......."}
Returns: 3
{".o..##",".#.##o"}
Returns: 1
{".#","##","##","##","o#","o#","#.","##","##","#.","##",".#","##"}
Returns: -1
{"#.....#..","........#","#........",".#.#..#..",".......#.",".........",".........","o........","......#..","......#.#","......o..",".........","...#.....",".......#."}
Returns: 1
{".........","...o.....","....#....",".o....#..",".........","........#"}
Returns: 2
{".##.##","###.##","####.#","####oo","##.#.#",".####.","#.##..","#.####",".###.#",".#.###",".#####",".#.###",".#..#.","##.#.#","######"}
Returns: 1
{"...o.#...",".........","#....#..#",".........",".........",".........",".......o.","....#....",".........",".........",".........","......##."}
Returns: 1
{"o####","#####","#####","#####","#####","#####","#####","###o#","#####","#####","#####","#####","#####"}
Returns: 1
{".##########o##",".#o.##########"}
Returns: 1
{"..##.#......#.##.","........##.#.....",".......#...#.#...","o..##...##.##.#..","..#..#...###.....",".###..#..#...#...","...#....#...#...#","..#####.....#..#.","##...#...##...#..","#.....##..##....#",".#.##...#........","####........#.#.#",".#...##.#o.....#."}
Returns: 1
{"#.##",".###","..##","...#","#.o.","##.#",".o##","...#","##..","#...","#...","####","##.#"}
Returns: 2
{".#....","#..#.#",".....#","......","#....#","..###o","#....#",".#....","#.####","......","##.#.#",".#...#",".#..#.","...##.","#..#..",".###o#"}
Returns: 1
{"#......##.##.##",".....##..#..###","..#..###.#.#..#","###o#####o#..##","..##......#..##"}
Returns: 2
{"...##.#..##...#.#","###.##.##.##.#.##",".##...#.###.....#",".##.#..#..##.#.#.","#..##..#.......#.","###.#......##..#.",".###...###...###.",".###.o#..#.###.#.",".#####..#..##.#.#","##.##..##..#....#",".#####.#.#####..#",".#.##.####.##o.##",".#####....#.#.##.","#####.##.#..#..#.","#..##.#.##.######"}
Returns: -1
{"#...#.....##..",".#.#.#........","#.#.#......#.#",".#.#......##..","....#....#....",".#.##...#...#.","...o.....###..","......#..#o...",".#...#.#..#...",".......#.##.#.","..............","...##..#......","...###.#.....#"}
Returns: 4
{"........","........","........","........","....#...","........","........","........","........","...o....","........","....#...",".....o.#","....#...","..#.....","........","#......#"}
Returns: 4
{"........","........",".....#..","........","........","........","........","........","...#....","........","o.......","o.......","......#.","#...#..."}
Returns: 4
{"....#..",".o#....","..##...","..#.#..","#...#.#","#...#..","...#...","....##.",".......","...#..#","...o...",".......",".......","....#..","......."}
Returns: 2
{".#..###.####.#.#.##","##...#.##.#.#..#.#.","#.##.#.#.###.......",".#....##........##.","#......#...#.#..###","..#.#.#..........#.","#....######.....##.",".....##......#....#","..####.####.....##.","..#..###.#.#....#..","#.......#.#....#...","...####.....o#...##","..#...#.####..#..#.","..####....#.##.###.","#..#.......#.##.##.","##......#.....#....","#..#.#.###....##o#.",".#.......##..#.#...","......#####......#."}
Returns: 3
{".##.#...##..",".#.##.....##","....###.....","#.#....o..#.",".##....##.#.",".#####..#.#.","##..#o#...#.",".##..#.....#","..###..###..",".##.###..###",".##..##.#...","#.#.##..#..#",".......#....","#......###..","#..#....#.#.","#.#....#.###","##.#.###.#.."}
Returns: 4
{".#...#","#.....","......","#..#..","......","..#o..","..o#..","....##","......","..#..."}
Returns: 3
{"...o..#......",".............",".............",".............",".............","...#.........",".............",".....#....#..",".............",".............",".............",".............",".............",".............",".......#.....",".............",".............",".............",".............","..#o........."}
Returns: 1
{"..o.","#o.#",".###","####","#.#.","##..","..##","####",".###","####"}
Returns: 1
{"#o###","##.#o","#.###"}
Returns: 1
{"#################.##","########.#.########.","##.#.#######.##.####","################.###","#.#####o############","#########.##########","#######.##.#########","####.######.#######.","########.####.######","######..#########.##","#########.##########","##################o#",".######.######.#####","##.#########.###.###","######.###.#####.###","####.#########.#####","#.###.##.###.#.###.#","###.################"}
Returns: -1
{"###..####.######.","########.########",".###.###..###.#.#",".#o####.#########","##########..#####","############.####","######.##.#######","#################","##.#######.####.#","#####.o.#####.###","#####.##.#.##.##.","########.###..###","#################","#############.###","##############.##","#.###############","#####.###########"}
Returns: -1
{"#...","....","###.","....","...#","o#.#","#.#.","..#.","..#.","o..#","...#","..#.","....","...#"}
Returns: 2
{"..........","........#.","...o......","..........","..........","........#.",".#........","..........","..........","#.......o.","..........",".........."}
Returns: 2
{"...o#","##.##",".#.#.","....#","...##",".#...","..o.#","##..."}
Returns: 1
{".#.....#..","#...#..#.#",".##.##...o","..##..#..#",".#...##...","....##....","###...#...","##..####.#","..o##....."}
Returns: 1
{"...........",".#...#.#..#","####.#.#.#.",".....#...##","..#..#.....","...#.#...##","..#.o..#..#","..#.#.###..",".o#.....#.#","##....#.#.#","..#..#...#.",".##.#..#...","#.###.#....",".#####.....","#..##.##.#.","..#....#..#","###..#.#..#"}
Returns: 2
{"####.","##..#","#.###","#####",".#.##","#####","#o###","o..##"}
Returns: 1
{"#.#.#","..#..","#.#.#","#.###","#..oo","....#","#.#..","###.#","#.##."}
Returns: 1
{"..#","#.#","#..","...","...",".#.",".#.","...","...",".o.","...","...","..o"}
Returns: 1
{"o.....","...#..","......","......","..o...","....#.","..#...","......","......","......","......","......","......","......"}
Returns: 1
{"##","##","##","##","##","##","##","##","##","##","##","#o","##","##","##","o#",".#"}
Returns: 1
{"...#...#...##..o###.","#.#..##..####...#.#.","....#.###......o#..."}
Returns: 1
{".#.##.#..##.##.####","#####.####.#...###o","##.#....##..#.#####",".###..##.######...#","#.##.##.##...#.##..","#####.#######.##.##","#.##.#####..###.###",".###..o############"}
Returns: 1
{"...###o#",".##.###.","#o..##.#","#.###.##"}
Returns: 1
{"...#",".#..","..o.","#...",".o..","#...","....",".#..",".#..","....",".#.#","....","....","....","#..#","...#","....",".##.","....","##.."}
Returns: 2
{"##########","...#o...##","..####...#","........##","##########","##########","...#o...##","..####...#","........##","##########"}
Returns: -1
{"##########","...#o...##","..####...#","........##","##########","##########","...#.o..##","..####...#","........##","##########"}
Returns: 10
{"##########","...#o...##","..####...#","........##","##########","##########","...#o...##","..####...#","........##","#######.##"}
Returns: 7
{"###", ".o.", "#.#", ".o.", "###" }
Returns: 3
{"....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", ".........o..........", "..........o.........", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "...................." }
Returns: 10
{".###", ".oo#", ".###" }
Returns: 2
{"############", ".........#o#", "############", "........o..#", "##########.#", "##########.#" }
Returns: 5
{"#######", "#.....#", "#.##.##", "#..#oo#", "##.####" }
Returns: 10
{"############", ".........#o#", "##########..", "..........o#", "############" }
Returns: 3
{"....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "..........o.........", ".........o..........", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "...................." }
Returns: 10
{"###################", "........o#.......##", "###################", "........o.........#", "###################", "..................#", "###################", "..................#", "###################", "..................#", "###################", "..................#", "###################", "..................#", "###################" }
Returns: 10
{"..####", "o.o..." }
Returns: 1
{"#####", ".o...", ".o...", "#####" }
Returns: -1