10154 - 跳棋的挑战

检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子。

列号

1 2 3 4 5 6

1 | | O | | | | |

2 | | | | O | | |

3 | | | | | | O |

4 | O | | | | | |

5 | | | O | | | |

6 | | | | | O | |

上面的布局可以用序列2 4 6 1 3 5来描述,第i个数字表示在第i行的相应位置有一个棋子,如下:

行号 1 2 3 4 5 6 列号 2 4 6 1 3 5

这只是跳棋放置的一个解。请遍一个程序找出所有跳棋放置的解。并把它们以上面的序列方法输出。解按字典顺序排列。请输出前3个解。最后一行是解的总个数。

特别注意: 对于更大的N(棋盘大小N x N)你的程序应当改进得更有效。不要事先计算出所有解然后只输出,这是作弊。如果你坚持作弊,那么你登陆USACO Training的帐号将被无警告删除

输入

一个数字N (6 <= N <= 13) 表示棋盘是N x N大小的。

输出

前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。

样例

输入

6

输出

2 4 6 1 3 5 
3 6 2 5 1 4 
4 1 5 2 6 3 
4

提示

Checker Challenge Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more than one in any diagonal. (Diagonals run from southeast to northwest and southwest to northeast and include all diagonals, not just the major two.)

      Column
1   2   3   4   5   6

1 | | O | | | | |

2 | | | | O | | |

3 | | | | | | O |

4 | O | | | | | |

5 | | | O | | | |

6 | | | | | O | |

The solution shown above is described by the sequence 2 4 6 1 3 5, which gives the column positions of the checkers for each row from 1 to 6:

ROW 1 2 3 4 5 6 COLUMN 2 4 6 1 3 5

This is one solution to the checker challenge. Write a program that finds all unique solution sequences to the Checker Challenge. Print the solutions using the column notation described above. Print the the first three solutions in numerical order, as if the queen positions form the digits of a large number, and then a line with the total number of solutions.

Special note: the larger values of N require your program to be especially efficient. Do not precalculate the value and print it (or even find a formula for it); that's cheating. Work on your program until it can solve the problem properly. If you insist on cheating, your login to the USACO training pages will be removed and you will be disqualified from all USACO competitions. YOU HAVE BEEN WARNED.

TIME LIMIT: 1 CPU second PROGRAM NAME: checker INPUT FORMAT A single line that contains a single integer N (6 <= N <= 13) that is the dimension of the N x N checkerboard. SAMPLE INPUT (file checker.in) 6

OUTPUT FORMAT The first three lines show the first three solutions found, presented as N numbers with a single space between them. The fourth line shows the total number of solutions found. SAMPLE OUTPUT (file checker.out) 2 4 6 1 3 5 3 6 2 5 1 4 4 1 5 2 6 3 4

时间限制 1 秒
内存限制 128 MB
讨论 统计
上一题 下一题