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 ...
随机推荐
- css 让两个div重叠
做网页的时候在div里放了一个别的网页的天气插件,但是点击了会跳到广告页面的,想去网上找个禁止div点击的方法,可是发现没有,用了js的方法好像也没有成功,后来觉得还是用两个层重叠的方法来阻止点击,虽 ...
- Maven引入Hadoop依赖报错:Missing artifact jdk.tools:jdk.tools:jar:1.6
Maven引入Hadoop依赖报错:Missing artifact jdk.tools:jdk.tools:jar:1.6 原因是缺少tools.jar的依赖,tools.jar在jdk的安装目录中 ...
- UNP学习笔记(第七章 套接字选项)
有多种方法获取和设置影响套接字的选项: 1.getsockopt和setsockopt函数 2.fcntl函数 3.ioctl函数 getsockopt和setsockopt函数 这两个函数仅用于套接 ...
- java与javax有什么区别?
http://zhidao.baidu.com/question/8702158.html java和javax都是Java的API包,java是核心包,javax的x是extension的意思,也就 ...
- ios面试基础
1.#import和#include的差别 @class? @class一般用于头文件里须要声明该类的某个实例变量的时候用到,在m文 件中还是须要使用#import 而#import比起#includ ...
- ubuntu16.04 下安装opencv2.4.9
准备工作,安装环境 sudo apt-get install build-essential cmake libgtk2.0-dev pkg-config python-dev python-nump ...
- linux下网卡绑定
网卡绑定的作用:1.冗余,防止单点故障 2.防止传输瓶颈 1.交换机端口绑定: system-view link-aggregation group 1 mode manual 比如把端口1和2进行绑 ...
- C#里类的get和set方法编写和调用
using System; class Date { int day; int month; int year; public int Day{ get { return day; } set { d ...
- VMware 中安装Centos
1,在百度软件中心下载VM12 http://rj.baidu.com/soft/detail/13808.html?ald 2,一路NEXT安装,安装完之后需要秘钥激活. VMware Workst ...
- 01 redis特点及安装使用
一:redis的特点 ()redis是一个开源,BSD许可高级的key-value存储系统.可以用来存储字符串,哈希结构,链表,集合,因此,常用来提供数据结构服务. 二:redis和memcached ...