Key Information

The challenge is finished.
Show Deadlines

Challenge Overview

Problem Statement



  • This is a test match. It means things can go wrong. We will surely try to fix everything as soon as possible, but please be aware that issues are possible. If you noticed an issue, please report it to us (
  • Testing of solutions is performed on VMs. It means that there exists certain variability between how much time the same algorithm can take to execute. We've done some tests and most of the time everything is fairly stable, but rarely we were seeing ~5% or even ~10% runtime differences. In theory, larger differences are also possible, we can't provide any guarantees here. If you notice that the same code runs with more than 10% different runtimes, we would be very interested to hear such information -- please email it to


There is a square board separated into NxN square cells. Rows of the board are numbered 0 to N-1 (top to bottom). Columns are numbered 0 to N-1 (left to right). Originally all cells of the board are empty.

You are given a String S that contains N*N characters. You need to place its characters (in the same order as they go within the string) to board's cells according to the following rules:

  • The first character can be placed into any cell.
  • Each next character can be placed into any empty cell that is adjacent (by side) to the cell where the previous character was placed.

In other words, you need to place the given string into the board so that it forms a Hamiltonian path.

Once all characters are placed, we will calculate C -- the number of connected components on the board. A connected component is a maximal (by inclusion) set of cells such that there exists a path between any two cells of this set. A path between X and Y is a sequence of cells that starts at X and ends at Y such that all cells (including X and Y) contain the same character and each two consecutive cells are adjacent (by side).

Your goal in this task is to minimize the value of C.

You will need to implement the method placeString. Its input is the string S. The return value must be an int[] containing N*N + 1 elements. First two elements are the row and the column where you place the first character. Each of the next N*N - 1 elements should be the direction (relative to the current cell) where you place the next character. 0 means right, 1 means up, 2 means left and 3 means down.

For each test case we will calculate your raw and normalized scores. If we were unable to receive solution from you (due to time limit, memory limit, crash, invalid return value, etc.), then both your raw and normalized scores are 0. Otherwise, the raw score will be equal to the value of C in your solution. The normalized score for each test is 1,000,000.0 * BEST / YOUR, where BEST is the lowest positive score currently obtained on this test case (i.e., considering the last submission from each competitor). Finally, your total score is equal to the arithmetic average of normalized scores on all test cases.

Test cases are generated as follows. N is selected uniformly, at random, between 30 and 100. A parameter A (alphabet size) is chosen uniformly, at random, between 3 and 5. Each character in S is selected uniformly and independently, at random, as one of first A letters of English alphabet. Only lowercase letters are used.

An offline tester/visualizer is available.



Method signature:int[] placeString(String S)
(be sure your method is public)


-The time limit is 20 seconds (this includes only the time spent in your code). The memory limit is 1024 megabytes.
-There is no explicit code size limit. The implicit source code size limit is around 1 MB (it is not advisable to submit codes of size close to that or larger). Once your code is compiled, the binary size should not exceed 1 MB.
-Some information about hardware and compilers used for this match can be found here.
-There are 10 example test cases and 100 full submission (provisional) test cases.


Returns: "Seed: 1
N: 82
A: 3"
Returns: "Seed: 2
N: 73
A: 3"
Returns: "Seed: 3
N: 81
A: 3"
Returns: "Seed: 4
N: 73
A: 3"
Returns: "Seed: 7
N: 64
A: 5"
Returns: "Seed: 10
N: 34
A: 4"
Returns: "Seed: 11
N: 44
A: 4"
Returns: "Seed: 12
N: 47
A: 5"
Returns: "Seed: 14
N: 89
A: 4"
Returns: "Seed: 25
N: 46
A: 5"

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2020, TopCoder, Inc. All rights reserved.