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");
}
}
PAT 甲级 1128. N Queens Puzzle (20) 【STL】的更多相关文章
- PAT甲级 1128. N Queens Puzzle (20)
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- PAT 甲级 1128 N Queens Puzzle
https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360 The "eight queens ...
- PAT甲级——A1128 N Queens Puzzle【20】
The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...
- PAT甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789810.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT 1128 N Queens Puzzle
1128 N Queens Puzzle (20 分) The "eight queens puzzle" is the problem of placing eight ch ...
- PAT 1128 N Queens Puzzle[对角线判断]
1128 N Queens Puzzle(20 分) The "eight queens puzzle" is the problem of placing eight chess ...
- PAT甲级:1152 Google Recruitment (20分)
PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...
- 1128 N Queens Puzzle (20 分)
The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...
- PAT 甲级 1041 Be Unique (20 分)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
随机推荐
- Qt编程简介与基本知识
1. 什么是Qt? Qt是一个基于C++的跨平台应用程序和UI开发框架.它包含一个类库,和用于跨平台开发及国际化的工具. 由挪威Trolltech公司开发,后被Nokia收购,目前被Digia公司收购 ...
- StringUtils和IOUtils工具包的使用
加载apache的工具类 <dependency> <groupId>commons-lang</groupId> <artifactId>common ...
- webstrom 代码工具(转http://www.cnblogs.com/tangdanni11/p/5149063.html)
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- java程序如何优化--技巧总结
http://www.douban.com/group/topic/17850695/
- Oracle 修改字段注释
修改字段注释SQL: COMMENT ON COLUMN 表名.字段名 IS '注释内容'; 批量修改所有表的字段注释,这里是修改所有没有注释的字段 附件: 实现步骤: ...
- linux下robotframework执行测试用例的几种方法
1.执行指定的测试用例文件(Test Suite) [root@localhost cases]# pybot purge.txt 2.执行整个porject目录下的所有测试用例 ...
- spring学习六----------Bean的配置之Aware接口
© 版权声明:本文为博主原创文章,转载请注明出处 Aware Spring提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化后,可以获取相应的资源 通过Aware接口,可以对S ...
- Redis(六):java里常用的redis客户端(Jedis和Redisson)
Redis的各种语言客户端列表,请参见Redis Client.其中Java客户端在github上start最高的是Jedis和Redisson.Jedis提供了完整Redis命令,而Redisson ...
- caffe---ubuntu1604下anaconda2.5的尝试----失败,建议使用系统的python系统,避免各种各样的陷阱
caffe---ubuntu1604下anaconda2.5的尝试----失败,建议使用系统的python系统,避免各种各样的陷阱. 如果使用caffe+ anacoanda 已经遇到的陷阱有 1. ...
- WebStorm 常用功能
WebStorm 常用功能的使用技巧分享 WebStorm 是 JetBrain 公司开发的一款 JavaScript IDE,使用非常方便,可以使编写代码过程更加流畅. 本文在这里分享一些常用功能的 ...