N-Queens

本文详细介绍了如何使用C++编程语言解决n皇后问题,并提供了算法实现的源代码。通过递归和回溯的方法,实现了n皇后问题的所有不同解决方案。代码中包括了打印棋盘状态和测试棋子放置的函数,展示了如何在n×n的棋盘上合理布局n个皇后,确保任意两个皇后不处于同一行、同一列或同一对角线上。
摘要由CSDN通过智能技术生成

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

For example,
There exist two distinct solutions to the 4-queens puzzle:

[
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]

C++:

class Solution {
public:
    
    void print(int queue[], int n, vector<vector<string>> &qipan) {
        int i, j;
        /*for(i = 0; i < n; i++) {
            printf("row :%d, col :%d\n", i , queue[i]);
        } */ 
        vector<string> method;
        for(i = 0; i < n; i++) {
            string str(n,'.');
            str[queue[i]] = 'Q';  
            method.push_back(str);
        }
            //printf("\n");
        qipan.push_back(method);
        //printf("\n");
    }

    void testQueue(int queue[], int row, int n, vector<vector<string>> &qipan) {
        int j, k;
        if(row >= n) print(queue, n, qipan);
        else {
            for(j = 0; j < n; j++) {
                queue[row] = j;
                for(k = 0; k < row; k++) {
                    if((queue[k] - queue[row]) * (fabs(queue[k] - queue[row]) - fabs(k - row)) == 0) {
                        break; 
                    }
                    if(k == row - 1) testQueue(queue, row + 1, n, qipan);
                }
            }
        }
    }

    vector<vector<string>> solveNQueens(int n) {
        vector<vector<string>> qipan;
        int queue[n];
        int i;
        for(i = 0; i < n; i++ ) queue[i] = -1;
        for(i = 0; i < n; i++) {
            //printf("method :\n");
            queue[0] = i;
            testQueue(queue, 1, n, qipan);
        }
        return qipan;
    }
};
C:

#include<stdio.h>
#include<math.h>

#define N 1

char qipan[100][N][N];
static int index = 0;

void print(int queue[], int n) {
    int i, j;
    for(i = 0; i < n; i++) {
        printf("row :%d, col :%d\n", i , queue[i]);
    }  
   
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            if(queue[i] == j)
                qipan[index][i][j] = 'Q';
            else qipan[index][i][j] = '.';
            printf("%c ", qipan[index][i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

void testQueue(int queue[], int row, int n) {
    int j, k;
    if(row >= n) print(queue, n);
    else {
        for(j = 0; j < n; j++) {
            queue[row] = j;
            for(k = 0; k < row; k++) {
                if((queue[k] - queue[row]) * (fabs(queue[k] - queue[row]) - fabs(k - row)) == 0) {
                    break; 
                }
                if(k == row - 1) testQueue(queue, row + 1, n);
            }
        }
    }
}

void solveNQueens(int n) {
    int queue[n];
    int i;
    for(i = 0; i < n; i++ ) queue[i] = -1;
    for(i = 0; i < n; i++) {
        printf("method :\n");
        queue[0] = i;
        testQueue(queue, 1, n);
    }
}

void main() {
    solveNQueens(N);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值