PAT 甲级 1128. N Queens Puzzle (20) 【STL】

题目链接

https://www.patest.cn/contests/pat-a-practise/1128

思路
可以 对每一个皇后 都判断一下 它的 行,列 ,左右对角线上 有没有皇后
深搜解决

但是这样太麻烦

其实我们可以想到

要符合要求的摆放
就是做到 每一行 每一列 每一条对角线 上 都是只有一个皇后的

每一行 就不用判断了

然后可以用 map 标记 该列 该对角线上 是否是已经有皇后的

如果已经有 那么就flag = 0

如果没有 就标记

然后 左对角线 上的特点就是 每个元素的 j - i 是相等的
右对角线上的特点是 i + j 是相等的

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;

const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30;

const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, num;
        int flag = 1;
        cin >> n;
        map <int, int> l, m, r;
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &num);
            if (m[num] || l[num - i] || r[num + i])
                flag = 0;
            m[num] = 1;
            l[num - i] = 1;
            r[num + i] = 1;
        }
        if (flag)
            printf("YES\n");
        else
            printf("NO\n");
    }

}






在C语言中,解决N皇后问题可以采用递归回溯法。N皇后问题是经典的计算机科学问题,目标是在一个N×N的棋盘上放置N个皇后,使得任意两个皇后都不在同一行、同一列,以及对角线上。以下是递归函数的基本思路: 1. 定义一个函数`isSafe(row, col)`检查当前位置是否安全,即该位置的行、列和左上到右下的对角线范围内是否有其他皇后。 2. 定义主函数`nQueens(int n)`,它接受一个整数n作为棋盘大小。 - 初始化:设置当前皇后的位置`col = 0`,并传递一个二维数组`board`表示棋盘状态(默认初始化全零)。 - 调用`placeQueen(n, row, board)`尝试将皇后放在第`row`行。 - 如果成功放置,继续在下一个行尝试;如果失败(比如`isSafe()`返回false),回溯到前一行,移除当前皇后,然后尝试下一个位置。 - 当所有皇后都放置好,说明找到一种解,输出或记录这个解决方案。 - 如果`row == n`,表示已处理完所有行,结束递归。 下面是伪代码形式: ```c int isSafe(int row, int col, int board[N][N]) { // 检查列冲突 for (int i = 0; i < row; ++i) if (board[i][col] == 1) return 0; // 检查左上到右下对角线冲突 int leftDiag = col - row + N - 1; for (int i = 0; i < row; ++i) if (board[i][leftDiag] == 1) return 0; // 检查右上到左下对角线冲突 int rightDiag = col + row; for (int i = 0; i < row; ++i) if (board[i][rightDiag] == 1) return 0; return 1; } void placeQueen(int n, int row, int board[N][N]) { if (row >= n) { // 解决方案找到,打印或记录棋盘 printSolution(board); return; } for (int col = 0; col < n; ++col) { if (isSafe(row, col, board)) { board[row][col] = 1; placeQueen(n, row + 1, board); // 递归放置下一行皇后 board[row][col] = 0; // 回溯:移除当前皇后 } } } int main() { int n = ... // 输入棋盘大小 int board[n][n]; nQueens(n, 0, board); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值