21645_NQueensPuzzle

2022-5-16 18:20| 发布者: Hocassian| 查看: 28| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-4-89505716886200-Problem List-采集的数据-后羿采集器.html

Pro.ID

21645

Title

N Queens Puzzle

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21645

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    The eight queens puzzle is a famous problem. We have to put eight queens on an 8 × 8 chessboard such that none of them is able to capture any other using the standard chess queen's moves (a queen can go to any place of the same row, the same column and the same diagonal). The queens must be placed in such a way that no two queens would be able to attack each other. Figure 1 shows a solution of the eight queens puzzle. For the more general N ( 8 ≤ N ≤ 300 ) queens puzzle, you should output K ( 1 ≤ K ≤ 20 ) different solutions.

    Figure 1

    输入

    The first line contains an integer T ( T ≤ 10 ), the number of test cases.

    For each test case, the first line contains two integers N and K, indicating the size of the chessboard and the number of solutions which you should output.

    输出

    Description

    The eight queens puzzle is a famous problem. We have to put eight queens on an 8 × 8 chessboard such that none of them is able to capture any other using the standard chess queen's moves (a queen can go to any place of the same row, the same column and the same diagonal). The queens must be placed in such a way that no two queens would be able to attack each other. Figure 1 shows a solution of the eight queens puzzle. For the more general N ( 8 ≤ N ≤ 300 ) queens puzzle, you should output K ( 1 ≤ K ≤ 20 ) different solutions.

    Figure 1

    Input

    The first line contains an integer T ( T ≤ 10 ), the number of test cases.

    For each test case, the first line contains two integers N and K, indicating the size of the chessboard and the number of solutions which you should output.

    Output

    For each test case, output the solutions like the samples below (each solution has N integers separated by blank characters). The solution is a permutation of { 1, 2, ..., n }. The number in the i-th place means that the i-th column queen is placed in the row with that number.

    Sample Input

    2
    8 2
    9 3

    Sample Output

    4 6 8 2 7 1 3 5
    3 6 8 1 4 7 5 2
    6 1 5 7 9 4 2 8 3
    9 4 6 8 2 7 1 3 5
    1 5 9 6 4 2 8 3 7

    Source

    样例输入

    2
    8 2
    9 3

    样例输出

    4 6 8 2 7 1 3 5
    3 6 8 1 4 7 5 2
    6 1 5 7 9 4 2 8 3
    9 4 6 8 2 7 1 3 5
    1 5 9 6 4 2 8 3 7

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部