Log inSign up
Problems / B055

Lighthouse lighting order

200 ptsCases 8Set by dongbinna
Added October 30.

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.

PlanLighthouse lit firstLighthouse lit secondLighthouse lit third
1123
2132
3213
4231
5312
6321

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.

PlanLighthouse lit firstLighthouse lit secondLighthouse lit thirdLighthouse lit fourth
11234
21243
31324
41342
51423
61432
72134

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.

N12345678
Number of possible orders126241207205,04040,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.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 8 cases to solve this.

Input
Expected output
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.

0 chars1 lineMemory 256MB (recorded only)Reference 38ms

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.