## Editorial for CPC '19 Contest 1 P5 - A Puzzlingly Puzzling Puzzle Problem 😎

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

#### Solution Sketch

For all subtasks, we can represent the game as a graph where each board configuration is a vertex. An board will have valid configurations. There are a number of ways to do this, such as indexing permutations, bitmask techniques, or strings.

##### Subtask 1

For the first subtask, there are only possible board configuration. We can randomly make moves for each player and track of the minimum moves for each player to reach the target board. If we do this enough times, we will have the correct answer with high probability.

**Time Complexity:** depends on implementation

##### Subtask 2

For the second subtask, we can perform a breadth first search from each player's starting board, for each game, and select the player with the least number of moves.

**Time Complexity:**

##### Subtask 3

For the third subtask, a key observation is that the starting board configuration does not change for each player. We can perform a single breadth first search from each player's starting board, creating a distance array. To determine the answer, we will check the distance array for each player and select the minimum.

**Time Complexity:**

##### Subtask 4

For the fourth subtask, we can perform a multi-source breadth first search from each of the players instead of a single-source breath first search. We will keep track of the distance and player for each vertex. The answer can then be determined in time for each game. This solution should work for subtasks 1 to 3 as well.

**Time Complexity:**

##### Subtask 5

For the last subtask, there is only a single game, but a very large graph. A few key observations are that the graph is fairly random and that since the starting and target boards are generated uniformly at random, we do not have to worry about the worst cases. Thus, we can perform a tridirectional breadth first search from the two starting board and ending board configurations. When a path starting from the target board intersects any of paths from the player's boards, we know we have found the minimum answer (in theory, we will have to keep checking all vertices at that depth to find the minimum player index, but with random data, it is very unlikely that the two players will have the same number of moves). By the birthday paradox, this will result in visiting approximately vertices (in practice, it is slightly more that that). Note that this solution will not pass subtask 4.

**Time Complexity:**

## Comments

An alternative solution for subtask 5 is to use IDA* algorithm. We can take the sum of Manhattan distances between the current locations and desired locations for each value in the puzzle as the heuristic. Also see: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1122 .