题目链接

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");
} }

PAT 甲级 1128. N Queens Puzzle (20) 【STL】的更多相关文章

  1. PAT甲级 1128. N Queens Puzzle (20)

    1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...

  2. PAT 甲级 1128 N Queens Puzzle

    https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360 The "eight queens ...

  3. PAT甲级——A1128 N Queens Puzzle【20】

    The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...

  4. PAT甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789810.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  5. PAT 1128 N Queens Puzzle

    1128 N Queens Puzzle (20 分)   The "eight queens puzzle" is the problem of placing eight ch ...

  6. PAT 1128 N Queens Puzzle[对角线判断]

    1128 N Queens Puzzle(20 分) The "eight queens puzzle" is the problem of placing eight chess ...

  7. PAT甲级:1152 Google Recruitment (20分)

    PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...

  8. 1128 N Queens Puzzle (20 分)

    The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...

  9. PAT 甲级 1041 Be Unique (20 分)

    1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...

随机推荐

  1. mac 安装 word2016并破解

    http://blog.csdn.net/zqbx7/article/details/53448280

  2. SEO误区之——静态化页面

    你随便去找一个做SEO的人或者一个公司,他百分之百会让你把网页弄成纯静态页面,然后告诉你这样对搜索引擎是如何如何地好,那么我告诉你,这个做 SEO的,肯定不专业. 网页静态化这个东西,纯属以讹传讹的事 ...

  3. UVA - 10895 Matrix Transpose

    UVA - 10895 Matrix Transpose Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & % ...

  4. Linux的经常使用命令(2) - 关机

    关机命令 shutdown‑h now 马上进行关机 shutdown‑r now 如今又一次启动计算机 -t sec : -t后面加秒数,即"过几秒后关机" -k      : ...

  5. UNP学习笔记(第三十章 客户/服务器程序设计范式)

    TCP测试用客户程序 #include "unp.h" #define MAXN 16384 /* max # bytes to request from server */ in ...

  6. TCP/IP详解 卷一(第二章 链路层)

    在TCP/IP协议族中,链路层主要有三个目的: 1.为IP模块发送和接收IP数据报 2.为ARP模块发送ARP请求和接收ARP应答 3.为RARP请求和接收RARP应答 TCP/IP支持多种不同的链路 ...

  7. 开发SharePoint 自定义WebService 的小工具

    是一个开源的项目,地址:http://www.codeproject.com/Articles/10728/WSS-Web-Service-DISCO-and-WSDL-Generator-Helpe ...

  8. 使用sphinx生成美观的文档

    先上效果图 详情 首先,须要知道什么是restructuredtext.能够理解为类似于markdown的一个东西. 然后 安装.pip install sphinx 进入存放文档的文件夹,在命令行, ...

  9. Ruby on Rails 路由解析

    为了更好的阅读体验.欢迎訪问 作者博客原文 Route是什么 Rails中URL的约定严格基于RESTful风格的.client的请求事实上是在操作一些资源.同一资源的不同的请求动作(GET, POS ...

  10. 11-利用session校验图片认证码

    /****************************************************************产生随机验证码的servlet******************** ...