|
|||||||
|
Chute
On a chute on a port wharf there are barrels in three colours: red, green and blue lying in random order. One is to put them in order so that the red barrels are in front (the lowest), then the blue ones follow, and the green ones are in the end (the highest). One may use a crane to arrange the barrels. The crane - in one move - can lift any three adjacent barrels from the chute and move them up. Then the barrels lying above roll down filling the gap and the crane places at the upper end of the chute the barrels taken. One is to schedule the crane's work dictating in the consecutive moves which triple of barrels should be moved up to the end. The arrangement of barrels on the chute is represented by a sequence of l letters c, n, z, where l is the number of barrels. The letters c, n, z represent a red ("czerwona" in Polish), blue ("niebieska") and green ("zielona") barrel, respectively. We assume the number of barrels l is not greater than 2000, and that there are at least 3 green barrels on the chute. The schedule of ordering the barrels by the crane can be written in a form of a finite sequence R = (r1, ..., rk) of positive integers not greater than l - 2. The i-th element ri of the sequence R is the position (counting from the bottom) of the lowest of the three barrels that are to be lifted and moved up to the end in the i-th move. ExampleSuppose we have 9 barrels arranged on the chute in the order: (red, green, blue, blue, red, blue, green, green, blue). They are represented by the sequence (c, z, n, n, c, n, z, z, n).The crane working according to the schedule R = (6, 2, 5) will change their arrangement as follows: (c, z, n, n, c, n, n, z, z), (c, c, n, n, z, z, z, n, n), (c, c, n, n, n, n, z, z, z), so after three moves they will be ordered red-blue-green. TaskWrite a program that:
For each initial arrangement of barrels (assuming that there are at least three green ones) there exist many schedules of ordering them red-blue-green. They may differ in the number of moves. Your program should find and write only one of them. The number of moves has not to be minimal for the solution to be correct. However, you should design your program to find schedules of ordering barrels in a possibly small number of moves and to find them as fast as possible. Input
Output
ExampleFor the file POC.IN:9 c z n n c n z z nThe following sequence in the file POC.OUT is a correct solution: 6 2 5 Your program should look for the file POC.IN in the current directory and create the file POC.OUT also in the current directory. The source file containing the program written by you should be named POC.???, where ??? are substituted by at most three-letter abbreviation of the programming language used. The same program in an executable form should be written in a file POC.EXE. |