1128. N Queens Puzzle (20) n皇后站位问题——对角线坐标判断
来源:互联网 发布:购销存软件 编辑:程序博客网 时间:2024/06/15 11:49
#include<cmath>#include<algorithm>#include<cstring>#include<iostream>#include<stack>#include<vector>#include<queue>#include<string>#include<map>using namespace std;//21min//耗时于思路/*************************题意:给出n皇后的站位,判断站位是否不冲突。*************************//************************求解要点:由于N=1000,查询要100次,若用暴力判断,N*N*M易超时实际上可以当输入排列位置时就能进行判断★每条左下方向斜线的坐标满足i+j相等★每条右下方向斜线的坐标满足i-j相等(为了不为负,多加个偏移量)每次输入一个位置,将其加入所处的行线、列线、左下线、右下线的若某个线大于1,说明已经冲突,flag置false.************************//********************************************/#define INF 0xfffffff //int32位,去除首位符号位,最大为该值#define M 3000int n,m;int row[M],ldown[M],rdown[M];int main(){int i,j;int m,n,c;scanf("%d",&m);bool flag;while(m--){scanf("%d",&n);memset(row,0,sizeof(row));memset(ldown,0,sizeof(ldown));memset(rdown,0,sizeof(rdown));flag=true;for(i=1;i<=n;i++){scanf("%d",&c);row[c]++;ldown[c+i]++;//左下斜线,一条线上的坐标满足c+i相等rdown[i-c+n]++; //右下斜线,一条线上的坐标满足c-i相等if(row[c]>1 || ldown[c+i]>1 || rdown[i-c+n]>1){flag=false;}}if(flag==true)cout<<"YES"<<endl;else cout<<"NO"<<endl;}return 0;}
阅读全文
0 0
- 1128. N Queens Puzzle (20) n皇后站位问题——对角线坐标判断
- poj3239 Solution to the n Queens Puzzle (n皇后问题)
- 1128. N Queens Puzzle (20)
- 1128. N Queens Puzzle (20)
- 1128. N Queens Puzzle (20)
- 1128. N Queens Puzzle (20)
- 1128. N Queens Puzzle (20)
- 1128. N Queens Puzzle (20)
- 1128. N Queens Puzzle (20)
- 1128. N Queens Puzzle (20)
- 1128. N Queens Puzzle (20)
- PAT 1128. N Queens Puzzle (20)
- PAT 1128. N Queens Puzzle (20)-甲级
- PAT 1128. N Queens Puzzle (20)
- PAT甲级 1128. N Queens Puzzle (20)
- PAT 1128. N Queens Puzzle (20)
- PAT 甲级 1128. N Queens Puzzle (20)
- 1128. N Queens Puzzle 解析
- Swift 继承
- dspic canmsg.arg2 = can_data[6] + (can_data[7] << 8);
- 53-Maximum SubArray
- VCC、VDD和VSS
- 1142:简单的事情
- 1128. N Queens Puzzle (20) n皇后站位问题——对角线坐标判断
- cs博士
- 初闻Python
- App错误日志记录到本地
- 黎明 V1.0版本发布
- 微信小程序授权获取用户详细信息openid
- 大数据早报:缺乏有效辟谣机制怎么办?人工智能狙击网络谣言 95后大数据报告出炉:兴趣决定一切(9.15)
- 每天一python 题 0002
- wampserver安装缺失vcruntime140.dll