Connection Status:
Competition Arena > CornersGame
SRM 308 · 2006-06-24 · by Andrew_Lazarev · Graph Theory
Class Name: CornersGame
Return Type: int
Method Name: countMoves
Arg Types: (vector<string>)
Problem Statement

Problem Statement

This problem contains an image that can be viewed in the applet.

A player has four draughts which are placed in the bottom right corner of a 6 x 6 chessboard. The draughts are arranged in a square with 2 rows and 2 columns (see picture below). Some cells on the board may contain red flags, and some may contain stones.

The player's goal is to move all the draughts to the top left corner and arrange them in a square using a minimal number of moves. The draughts are indistinguishable, so their order in the final position doesn't matter. The target cells are guaranteed to be free. There are two kinds of moves that can be made by a draught. The first is to move to any vertically or horizontally adjacent free cell. The second is to jump over a single vertically or horizontally adjacent draught or stone into a free cell. The player can never move a draught into a cell that contains a flag, stone or other draught and he can never jump over a flag or an empty space.

You will be given a String[] board where each element represents a single row of the chessboard. The rows are given from top to bottom, and each row is given from left to right. '.' represents a free cell, 'r' represents a cell with a red flag, and 's' represents a cell with a stone. Return the minimal number of moves necessary to reach the goal, or -1 if it is impossible.

Constraints

  • board will contain exactly 6 elements.
  • Each element of board will contain exactly 6 characters.
  • Each element of board will contain only the characters 'r', 's', and '.'.
  • board will contain '.' characters at the initial and desired final positions of the draughts.
Examples
0)
{"......", 
 "......",
 "......",
 "......",
 "......",
 "......"}
Returns: 16
1)
{".....s",
 "..s.r.",
 "r.....",
 ".srs..",
 "..r...",
 "......"}
Returns: 19

The board shown on the picture above.

2)
{"......",
 "......",
 "....ss",
 "....ss",
 "...r..",
 "...r.."}
Returns: -1

We can not make any move.

3)
{"...s.r",
 "..r.s.",
 "rr.s..",
 "..s.rr",
 "s.rr..",
 ".s.s.."}
Returns: 54
4)
{"......","...rr.","rrr...","....rr",".rrr..","......"}
Returns: 51

Submissions are judged against all 56 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class CornersGame with a public method int countMoves(vector<string> board) · 56 test cases · 2 s / 256 MB per case

Submitting as anonymous