P2105 K皇后
2024-10-21 07:43:54
题意:$n*m$棋盘放置k个皇后,问几个格子不被攻击 1≤n,m≤20000,1≤k≤500
开set判重暴力$O(n*k)$然而,setMLE了QAQ
正解确实是$O(n*k)$的
以hang[i]记录此行是否被占用
用c[i]动态维护没被占用的行有几个安全的,(枚举皇后打标记)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<set>
using namespace std;
#define olinr return
#define _ 0
#define love_nmr 0
#define DB double
int x[];
int y[];
bool hang[];
bool c[];
inline int read()
{
int x=,f=;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
f=-f;
ch=getchar();
}
while(isdigit(ch))
{
x=(x<<)+(x<<)+(ch^);
ch=getchar();
}
return x*f;
}
inline void put(int x)
{
if(x<)
{
x=-x;
putchar('-');
}
if(x>)
put(x/);
putchar(x%+'');
}
int n;
int m;
int k;
int ans;
int main()
{
n=read();
m=read();
k=read();
for(int i=;i<=k;i++)
{
x[i]=read();
y[i]=read();
hang[x[i]]=true;
}
for(int i=;i<=n;i++)
{
if(!hang[i])
{
memset(c,,sizeof c);
int sum=m;
for(int j=;j<=k;j++)
{
if(!c[y[j]]) //枚举所占列,sum--
{
c[y[j]]=true;
sum--;
}
if(y[j]+x[j]-i>=&&y[j]+x[j]-i<=m&&!c[y[j]+x[j]-i]) //自己推一下,神奇的事情发生了! 这居然是皇后左下到右上对角线与当前行交点的y!!!!!!!
{
c[y[j]+x[j]-i]=true;
sum--;
}
if(y[j]-x[j]+i>=&&y[j]-x[j]+i<=m&&!c[y[j]-x[j]+i]) //这里是从右下到左上的交点
{
c[y[j]-x[j]+i]=true;
sum--;
}
}
ans+=sum; //统计安全~~~~~的
}
}
put(ans);
olinr ~~(^_^)+love_nmr;
}
最新文章
- 各种HTTP状态的含义
- Fragment全解析系列(一):那些年踩过的坑
- WCF	异步调用问题
- 《DDNS服务器的搭建和案例解决方法》
- Case when 的用法,简单Case函数
- dfs+dp思想的结合------hdu1078
- salesforce 零基础学习(六十六)VF页面应善于使用变量和函数(二)常用函数的使用
- javascript定义二维数组与添加
- zabbix部署
- js检测数据类型四种办法
- Linq(高级查询)
- cad2016卸载/安装失败/如何彻底卸载清除干净cad2016注册表和文件的方法
- oracle查询2G以上的表
- Spring-IOC MethodInvokingFactoryBean 类源码解析
- poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数
- 什么是集群(Cluster)技术
- Python并行编程(十二):进程同步
- APP纯黑盒测试—某些可以试试的操作
- 文本处理三剑客之 Sed &mdash;&mdash;高级编辑命令
- HDU 4609 FFT模板