Hot Spot

Time limit1sMemory limit128 MB

Summary
On a 4 by 4 board, robots jump over one or two adjacent robots into an empty square; find the fewest jumps that bring the red robot to the top-left corner without violating the blue-robot adjacency rule.
Level

Medium7 of 10

Topics
BFS, Graph, Simulation, Implementation
Solved
No attempts yet

Problem

Hot Spot is a single-player game played on a 4 by 4 board. The goal is to move a red robot from its current square to the top-left corner. The board may also contain green and blue robots. At any time, each square holds at most one robot.

A robot may move in one of two ways:

  1. If two robots are adjacent horizontally or vertically (never diagonally), one may jump over the other into the square immediately beyond, provided that square is empty. For example, if robot aa is immediately to the left of robot bb, then aa may jump to the square immediately to the right of bb.
  2. If three robots are in a row horizontally or vertically (again never diagonally), one of them may jump over the other two into the square immediately beyond, provided that square is empty. For example, if bb is immediately to the right of aa and cc is immediately to the right of bb, then aa may jump to the square immediately to the right of cc.

Every jump only changes the position of the jumping robot; robots are never removed from or added to the board.

A blue robot may never be adjacent, horizontally or vertically, to another blue robot or to the red robot; no move may create such an adjacency.

Given the initial board, determine the minimum number of jumps needed to bring the red robot to the top-left corner.

Input

The input describes the initial board as four lines, each containing four characters. Each character is one of: R for the red robot, B for a blue robot, G for a green robot, or . (a period) for an empty square.

Output

Output a single line with one integer: the minimum number of jumps needed for the red robot to reach the top-left square of the board.

Examples1

  1. Example 1

    Input
    .GR.
    ....
    ....
    ....
    
    
    Expected output
    1