Two Coins Unlimited
InterviewTime limit0.5sMemory limit512 MB
Two coins sit on a grid with walls; each button press slides both coins the same direction, and you must find the fewest presses that drop exactly one coin off the board.
- Level
Medium6 of 10
- Topics
- BFS, Graph, Simulation, Implementation
- Solved
- No attempts yet
Problem
There is a game played on an board with 4 buttons. The board is divided into square cells, and each cell is either empty or a wall. Two empty cells each contain one coin, and the two coins are in different positions.
The four buttons are "left", "right", "up", and "down". When a button is pressed, both coins move at the same time in the direction written on the button.
- If the cell a coin would move into is a wall, the coin does not move.
- If there is no cell in the direction a coin would move, the coin falls off the board.
- Otherwise, the coin moves one cell in that direction. It also moves one cell when the cell it would move into contains a coin.
Write a program that finds the minimum number of button presses needed to make exactly one of the two coins fall off the board.
Input
The first line gives the board's height and width . ()
The next lines give the state of the board.
o: coin.: empty cell#: wall
The number of coins is always 2.
Output
On the first line, print the minimum number of button presses needed to make exactly one of the two coins fall off the board. If it is impossible to make a coin fall off, print -1.