n皇后-输出所有情况C++

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
/*
 CLion 使用提示 :

  批量注释代码 => Ctrl + shift + /
  格式化代码   => option + command + L
  补全语句     => Shift + command + return
  生成构造器,getter和setter方法等 => control + return
  行注释       => control + /
  块注释       => control + shift + c
*/
constexpr const unsigned SIZE = 100;
int n = 0, x[SIZE], sum = 0;
vector<vector<string>> ans = // ?
{
        {"!", "!", "!", "!", "!", "!", "!", "!", "!"},
        {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
        {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
        {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
        {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
        {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
        {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
        {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
        {"!", "x", "x", "x", "x", "x", "x", "x", "x"}
};
void initString() {
    ans = {
            {"!", "!", "!", "!", "!", "!", "!", "!", "!"},
            {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
            {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
            {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
            {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
            {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
            {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
            {"!", "x", "x", "x", "x", "x", "x", "x", "x"},
            {"!", "x", "x", "x", "x", "x", "x", "x", "x"}
    };

}

void printSolution() {
    vector<int> v;
    v.push_back(0);//随便推入1个元素,然后实际上有用的元素是下标从1开始的
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            ans[i][j] = "x";
        }
    }
    for (int i = 1; i <= n; ++i) {
        ans[i][x[i]] = "√";
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            printf("%s", ans[i][j].c_str());
        }
        printf("\n");
    }
}

bool Place(int k) {
    for (int j = 1; j < k; ++j) {
        if (abs(k - j) == abs(x[j] - x[k]) || x[j] == x[k]) {
            return false;
        }
    }
    return true;
}

void BackTract(int t) {
    if (t > n) {
        int w = sum++;
        printf("第%d个解\n" , w+1);
        printSolution();
        printf("\n");
    } else {
        for (int i = 1; i <= n; ++i) {
            x[t] = i;//第t个皇后放在第i列
            if (Place(t)) {
                BackTract(t + 1);
            }
        }
    }
}

void solution() {
    unsigned T = 0;

    while (true) {
        printf("请输入一个正整数T,代表运行算法T次\n");
        scanf("%d", &T);
        for (int i = 1; i <= T; ++i) {
            printf("请输入皇后个数n\n");
            scanf("%d", &n);
            BackTract(1);
        }
    }

}

int main() {
    solution();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的 n-皇后问题的程序示例,使用 C++ 语言编写: ```c++ #include <iostream> #include <vector> using namespace std; void dfs(int n, vector<int>& cols, vector<vector<string>>& res) { if (cols.size() == n) { vector<string> board(n, string(n, '.')); for (int i = 0; i < n; i++) { board[i][cols[i]] = 'Q'; } res.push_back(board); return; } for (int col = 0; col < n; col++) { bool ok = true; for (int i = 0; i < cols.size(); i++) { if (cols[i] == col || cols[i] - i == col - cols.size() || cols[i] + i == col + cols.size()) { ok = false; break; } } if (ok) { cols.push_back(col); dfs(n, cols, res); cols.pop_back(); } } } vector<vector<string>> solveNQueens(int n) { vector<vector<string>> res; vector<int> cols; dfs(n, cols, res); return res; } int main() { int n; cin >> n; vector<vector<string>> res = solveNQueens(n); for (auto board : res) { for (auto row : board) { cout << row << endl; } cout << endl; } return 0; } ``` 其中,程序使用一个递归函数 dfs 来搜索所有可能的解法。每一个递归层次表示已经摆放了前 i 行的皇后,cols 数组表示每一行皇后所在的列数。在每一层递归中,程序枚举当前行的每一列,检查是否可以放置皇后。具体来说,程序使用一个布尔变量 ok 来表示当前列是否可以放置皇后,遍历已经放置的每一个皇后,检查是否在同一列,同一对角线上,如果是,则将 ok 设置为 false,并且跳出循环。如果 ok 为 true,表示当前列可以放置皇后,程序将当前列加入 cols 数组中,继续递归搜索下一层,搜索完毕后,需要将当前列从 cols 数组中弹出,以便于搜索其他解法。 程序最终输出所有的解法,每一个解法是一个 n*n 的棋盘,其中 Q 表示皇后,. 表示空格。 注意:本程序没有进行输入数据的合法性检查,实际应用中需要进行更加严格的输入数据处理。此外,当 n 比较大时,程序的搜索时间会比较长,因此需要使用更加高效的算法来解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿维的博客日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值