Connection Status:
Competition Arena > NinePuzzle
SRM 498 · 2010-11-01 · by ir5 · Greedy, Search
Class Name: NinePuzzle
Return Type: int
Method Name: getMinimumCost
Arg Types: (string, string)
Problem Statement

Problem Statement

Fox Ciel invented a game called Nine Puzzle. The game is played on a board which is in the form of a regular triangle with side length 4. There are 10 cells on the board, each in the form of a regular triangle with side length 1. The cells are numbered 0 to 9 as shown in the picture below:



Nine of the cells on the board contain a triangular piece. Each piece is painted red, green, blue or yellow. The remaining tenth cell is empty. The goal of the game is to arrange the cells to match a specified goal pattern. To do this, the player can perform the following move any number of times: Choose a piece which is adjacent to an empty cell, and move the piece into that empty cell. Two cells are considered adjacent if the distance between their centers is exactly 1. An example of a valid move is shown in the following picture:



Ciel has painted the triangular pieces to form the starting pattern for the puzzle. She has also chosen the goal pattern. However, she has chosen both these patterns arbitrarily, so it's possible that the puzzle might not be solvable (i.e., it may be impossible to achieve the goal pattern from the given starting pattern using a sequence of valid moves). If this is the case, she would like to repaint the minimum possible number of pieces in the starting pattern to make the puzzle solvable.

You are given two Strings init and goal. The i-th (0-indexed) character of init describes the content of the i-th cell in the starting pattern. The '*' character denotes an empty cell. 'R', 'G', 'B' and 'Y' denote pieces which are colored red, green, blue and yellow, respectively. The goal pattern is described in the String goal in the same format.

Return the minimum number of pieces which must be repainted in the starting pattern to make the puzzle solvable. If the puzzle is already solvable without repainting, return 0.

Constraints

  • init and goal will each contain exactly 10 characters.
  • Each character of init and goal will be one of 'R', 'G', 'B', 'Y' or '*'.
  • init and goal will each contain exactly one '*' character.
Examples
0)
"BG*YRGRRYR"
"BGGY*YRRRR"
Returns: 0

No repainting is required because Ciel can achieve the goal pattern from the starting pattern in 3 moves:

1)
"GBBB*BGBBG"
"RYYY*YRYYR"
Returns: 9

Ciel incautiously chose the starting pattern and the goal pattern, so unfortunately, she must repaint all the pieces.

2)
"RRBR*BRBBB"
"BBRB*RBRRR"
Returns: 1
3)
"RRRRRRRRR*"
"RRRRRRRRR*"
Returns: 0

init==goal case.(part1)

4)
"YBB*GRGYGB"
"YBB*GRGYGB"
Returns: 0

init==goal case.(part2)

5)
"YYBBGGGRR*"
"*RRRBBYYGG"
Returns: 1

2,2,2,3 case

6)
"RYYGYB*YYY"
"BYBB*GBBRB"
Returns: 5

1,1,1,6 case

14)
"RRRRRRR*RR"
"BYGYBGB*YG"
Returns: 9

res=9

20)
"*YYGYYYYBR"
"YY*GRYBYYB"
Returns: 1

random cases

50)
"YBG*RBYGRB"
"GGYBYR*BRY"
Returns: 1

{R,G,B,Y} = {2,2,2,3} cases

60)
"R*GGGGBGGY"
"*RGGGGBGGY"
Returns: 0

swapping at corner position R * G G G G B G G Y * R G G G G B G G Y

61)
"RG*GGGBGGY"
"*GRGGGBGGY"
Returns: 0

R G * G G G B G G Y * G R G G G B G G Y

62)
"RGG*GGBGGY"
"RGGBGG*GGY"
Returns: 0

R G G * G G B G G Y R G G B G G * G G Y

63)
"RGGGGGB*GY"
"RGGGGG*BGY"
Returns: 0

R G G G G G B * G Y R G G G G G * B G Y

64)
"RGGGG*BGGY"
"RGGGGYBGG*"
Returns: 0

R G G G G * B G G Y R G G G G Y B G G *

65)
"RGGGGGBG*Y"
"RGGGGGBGY*"
Returns: 0

R G G G G G B G * Y R G G G G G B G Y *

66)
"*BRRYRGRYR"
"BGGRB*BGBG"
Returns: 6

res = 6,7,8

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

Coding Area

Language: C++17 · define a public class NinePuzzle with a public method int getMinimumCost(string init, string goal) · 91 test cases · 2 s / 256 MB per case

Submitting as anonymous