Lighthouse lighting order
Statement
N lighthouses numbered 1 through N stand along a coastline. The station lights all N of them once each every night, and because of the capacity of the generator it can light only one at a time. It therefore has to settle in advance which lighthouse is lit in which place.
There are many orders in which all of the lighthouses can be lit once each. The station wants to write down every possible order, leaving none out, and make a lighting schedule out of them.
Write a program that prints every order that lights all N lighthouses once each, in lexicographic order. Lexicographic order means that an order whose first lighthouse has the smaller number comes first, and when those numbers are equal, the order whose next lighthouse has the smaller number comes first.
For example, suppose N=3. There are 6 possible lighting orders, and listing them in lexicographic order gives the following table.
| Plan | Lighthouse lit first | Lighthouse lit second | Lighthouse lit third |
|---|---|---|---|
| 1 | 1 | 2 | 3 |
| 2 | 1 | 3 | 2 |
| 3 | 2 | 1 | 3 |
| 4 | 2 | 3 | 1 |
| 5 | 3 | 1 | 2 |
| 6 | 3 | 2 | 1 |
Compare plan 3 in the table with plan 1. All three lighthouses that get lit are the same, but the order in which they are lit differs, so the two are different plans. Both plans are therefore in the table.
With N=4 there are 24 orders. Writing out only the first seven of them gives the following table.
| Plan | Lighthouse lit first | Lighthouse lit second | Lighthouse lit third | Lighthouse lit fourth |
|---|---|---|---|---|
| 1 | 1 | 2 | 3 | 4 |
| 2 | 1 | 2 | 4 | 3 |
| 3 | 1 | 3 | 2 | 4 |
| 4 | 1 | 3 | 4 | 2 |
| 5 | 1 | 4 | 2 | 3 |
| 6 | 1 | 4 | 3 | 2 |
| 7 | 2 | 1 | 3 | 4 |
Only once every plan that lights lighthouse 1 first has been written down does a plan that starts with 2 come up. Carrying on like this, the last plan, plan 24, is 4, 3, 2, 1.
As N grows, the number of orders grows quickly. The number of orders for each N is as in the following table.
| N | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Number of possible orders | 1 | 2 | 6 | 24 | 120 | 720 | 5,040 | 40,320 |
Input and Output Examples
Example 1
Input
3
Output
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
Example 2
Input
1
Output
1
Example 3
Input
2
Output
1 2 2 1
Input
The number of lighthouses N standing along the coastline is given first as a natural number. N is a natural number from 1 to 8.
The input ends with this one line, and that line holds nothing but N. The lighthouse numbers run from 1 through N, so the numbers are not given separately. If N is 8 there are 40,320 possible orders, so the number of lines printed grows to match.
Output
Print every order that lights all N lighthouses once each in lexicographic order, one per line. One line holds the N lighthouse numbers in the order they are lit. On top of that, The last three characters of the output are therefore a space, a newline and a newline, in that order. When a line holds more than one value, the values are separated by a single space.
Requirements
- Every lighthouse is lit once each, with none left out
- Print the orders in lexicographic order
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 8 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 8 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 8 cases to solve this.
1
1
2
1 2 2 1
3
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
Nobody yet. You could be the first.