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 ...
随机推荐
- mac搭建安卓开发环境
下载 android studio,利用android studio自带sdk manager下载安卓sdk,在sdk manager中设置相关代理地址,下载完sdk后,在bash_profile中把 ...
- angular 视频教程
在网上找了一些,视频教程.存在备用 angular 视频教程 百度云盘地址 小时前 1小时前 30 6 angular 4.0视频教程 链接:https://pan.baidu.com/s/1qXIt ...
- vscode 折叠所有区域
- Shiro学习(7)与Web整合
Shiro提供了与Web集成的支持,其通过一个ShiroFilter入口来拦截须要安全控制的URL.然后进行对应的控制,ShiroFilter相似于如Strut2/SpringMVC这样的web框架的 ...
- CentOS7.1 KVM虚拟化之经常使用管理虚拟机命令(3)
一.查看虚拟机列表及状态 [root@kvm01 ~]# virsh list --all Id Name State ---------------------------------------- ...
- oled屏幕
oled作为一种新型的有机显示屏,越来越现实出其重要性,它不但超薄可弯折并且可视视角较宽.处在不论什么角度看屏幕都不会造成图像的失真. 且它有三中原色:绿.红,蓝. 我近期在学安在智能车上的oled ...
- int a; int* a; int** a; int (*a)[]; int (*a)(int)
a) int a;表示一个内存空间,这个空间用来存放一个整数(int):b) int* a;表示一个内存空间,这个空间用来存放一个指针,这个指针指向一个存放整数的空间,即a)中提到的空间:c) int ...
- 动态PPT制作
今天开通的博客,希望以后能够和大家一起分享学习心得.今天也是第一次学习制作动态PPT. 如果想要做成flash那种效果,建议学习下<动画传奇>这本书. 做成flash效果,需要用到动画中的 ...
- Linux安装indicator-china-weather
https://launchpad.net/indicator-china-weather sudo apt-get update sudo apt-get install python-appind ...
- Chrome禁用NPAPI插件(包含 Silverlight、Java 和 Unity)
过去,很多插件都是使用一种称为NPAPI 的旧系统开发的. 现在,仅仅有少量站点在使用NPAPI 插件,由于这些插件有时会给站点带来安全风险. 为了让用户获得更安全.更高速且更稳定的 Chrome 浏 ...