||Happy New Year!
My solution looks almost the same as nika's.
I split the field into 3x3 - 5x5 sub-rectangles. The number of sub-rectangles should be even, either vertically or horizontally in order to create a hamilton cycle easily.
I calculated all the chain patterns for each sub-rectangle size beforehand. Then I used simulated annealing. These two were neighbors ,
(1) Randomly picked up a sub-rectangle and chose a different chain.
(2) Randomly picked up a border of sub-rectangles, moved a position where two chains are connected on the border.
Regarding color, if the number of colors was three or four, the color was trivial. You can use any algorithm to assign them. In the case of two colors, they were assigned to the black or white cell of the checkerboard and they must be the same number. I took it into account during simulated annealing.
Once I reached 976232.19, which seemed the optimal score as Milanin and sdya got. However, in my local test, I failed to reach the optimal, 4 out of 300 cases . So, I re-submitted my solution and my rank went down to the 3rd.
> 2*min(c, c) + 2*min(c, c) + c + c - (c+c) % 2
At first, I thought it was optimal length. However, I guess it would be length 2 shorter than the above length on the following cases ,
- min(c, c) = even, min(c, c) = even. c = even. c = odd.
- min(c, c) = even, min(c, c) = even. c = odd. c = even.
- min(c, c) = odd, min(c, c) = odd. c = even. c = even.
- min(c, c) = odd, min(c, c) = odd. c = odd. c = odd.
For example, c=1, c=1, c=1, c=1, c=1, c=1,
2*min(c, c) + 2*min(c, c) + c + c - (c+c) % 2 = 6
However, I can only create a cycle of length 4 at most. I don't know why, so I hope someone will prove it instead. :)
I have just posted my solution for TCO 2017 Announcement Fun Round. I am sorry to late.