Register
Submit a solution
The challenge is finished.

Challenge Overview

Important Links

  • Submission-Review You can find your submissions artifacts here. Artifacts will contain output.txt's for both example test cases and provisional test cases with stdout/stderr and individual test case scores.
  • Other Details For other details like Processing Server Specifications, Submission Queue, Example Test Cases Execution.

Overview

Jewels is a game where you must construct horizontal or vertical lines of the same type of jewel on a NxN grid. Each cell of the grid is one of C types of jewels. In each move you can swap any two jewels. Jewels are removed from the grid when they form a horizontal or vertical line of at least 3 matching jewels next to each other. Jewels above empty spaces fall down and can form more matching horizontal or vertical lines which will again be removed and the process repeats. Such an event is called a chain reaction. The more chain reactions you create, the more points you will score. Your task is to play the game for 1000 moves and remove as many jewels from the grid with as many chain reactions as you can.

Here is a partial animation of a solution for seed=1.

Input and Output

This is an interactive problem, so your code needs to interact with the tester for each move. Initially your code will receive as input the following values, each on a separate line:
  • N, the size of the grid.
  • C, the number of jewel types.
  • N*N lines describing the grid in row-major order. Each line containing the 1-based jewel type. The first row is at the bottom and the last row at the top.
The following game loop repeats until the game terminates after 1000 moves:
  • Your solution sends moves by writing a line to output. Your move should be formatted as "R1 C1 R2 C2" (without the quotes), which means that you want to swap the jewels at locations (R1,C1) and (R2,C2). These coordinates are 0-based. You cannot swap a cell with itself. Row 0 is at the bottom and Row N-1 at the top. Column 0 is on the left.
  • The tester will then:
    • 1. Swap the two jewels.
    • 2. Set the number of chain reactions and move score to 0.
    • 3. Mark all the jewels that form part of a horizontal or vertical line with length greater than two. Increment your move score according to each line found (Scoring explained in the scoring section).
    • 4. Remove all the marked jewels.
    • 5. Let all the jewels fall down until no jewel can fall further. New jewels will also enter from the top.
    • 6. Increment the number of chain reactions if any jewels were marked.
    • 7. Goto step 3 until no jewels were removed.
    • 8. Multiply your move score by the number of chain reactions and add this to your total score.
  • After each move you will receive an updated grid from the tester, with N*N lines describing the grid.
  • On the next line the tester will provide you with "runtime" (without the quotes), where runtime is the total time spent in your solution in milliseconds.

Scoring

The raw score is the total score you achieved over all moves. The score of a single move is calculated by multiplying the sum of all line scores with the number of chain reactions for that move. If the length of a matching jewel line is L, then the line score will be (L-2)*(L-2). For example, 3 jewels in a row will score 1 point and 4 jewels will score 4 points.

If your return was invalid, then your raw score on this test case will be -1. Possible reasons include:
  • Not formatting the moves correctly.
  • Using coordinates that are out of bounds.
  • Trying to swap a cell with itself.
  • Not making exactly 1000 moves.
If your raw score for a test case is negative then your normalized score for that test case is 0. Otherwise, your normalized score for each test case is YOUR/MAX, where YOUR is your raw score and MAX is the largest positive raw score currently obtained on this test case (considering only the last submission from each competitor). Finally, the sum of all your test scores is normalized to 100.

Test Case Generation

Please look at the generate() method in visualizer's source code for the exact details about test case generation. Each test case is generated as follows:
  • The grid size N is chosen between 8 and 16, inclusive.
  • The number of jewel types C is chosen between 5 and 10, inclusive.
  • The jewel grid of 1000*N rows and N columns is randomly created. Your solution will only be able to see the lower N rows of the grid. A larger grid is created such that each test case has consistent new jewels falling in for each column.
  • All values are chosen uniformly at random.

Notes

  • The time limit is 10 seconds per test case (this includes only the time spent in your code). The memory limit is 1024 megabytes.
  • The compilation time limit is 30 seconds.
  • There are 10 example test cases and 100 full submission (provisional) test cases. There will be 2000 test cases in the final testing.
  • The match is rated.

Languages Supported

C#, Java, C++ and Python

Submission Format

Your submission must be a single ZIP file not larger than 500 MB, with your source code only.
Please Note: Please zip only the file. Do not put it inside a folder before zipping, you should directly zip the file.

Make sure you name your Source Code file as Jewels.<appropriate extension>

SAMPLE SUBMISSIONS

Here are example solutions for different languages, modified to be executed with the visualizer. You may modify and submit these example solutions:

Tools

An offline tester is available below. You can use it to test/debug your solution locally. You can also check its source code for an exact implementation of test case generation and score calculation. You can also find links to useful information and sample solutions in several languages.

DOWNLOADS

OFFLINE TESTER / VISUALIZER

In order to use the offline tester/visualizer tool for testing your solution locally, you'll have to include in your solution the main method that interacts with the tester/visualizer via reading data from standard input and writing data to standard output.

To run the tester with your solution, you should run:

java -jar tester.jar -exec "<command>" -seed <seed>

Here, <command> is the command to execute your program, and <seed> is seed for test case generation.
If your compiled solution is an executable file, the command will be the full path to it, for example, "C:\TopCoder\Jewels.exe" or "~/topcoder/Jewels".
In case your compiled solution is to be run with the help of an interpreter, for example, if you program in Java, the command will be something like "java -cp C:\TopCoder Jewels".

Additionally you can use the following options:
  • -seed <seed> Sets the seed used for test case generation, default is seed 1.
  • -debug. Print debug information.
  • -novis. Turns off visualisation.
  • -delay <delay>. Sets the delay (in milliseconds) between visualizing consecutive simulation steps, default is 200.
  • -pause. Starts visualizer in paused mode. See more information below.
  • -manual. Play the game manually using the mouse. Left click to select one cell, and left click again on a cell to swap it with the first one. Clicking the same cell deselects it.
  • -N <N> Sets a custom grid size.
  • -C <M> Sets a custom number of jewel types.
The visualizer works in two modes. In regular mode, steps are visualized one after another with a delay specified with the -delay parameter. In paused mode, the next move will be visualized only when you press any key. The space key can be used to switch between regular and paused modes. The default starting mode is regular. You can use the -pause parameter to start in paused mode. You can also play the game manually using the -manual parameter.

Marathon local testers have many useful options, including running a range of seeds with a single command, running more than one seed at time (multiple threads), controlling time limit, saving input/output/error and loading solution from a file. The usage of these options are described here.