P1219 八皇后 (dfs 表格坐标关系)

一个正常的dfs(数据范围1-13),发现一条对角线上,分别符合和与差相等。因为有负数,这里我最开始开的是map,发现卡了最后一个点TLE,记录一下时间复杂度(

map,set的时间复杂度:logN

传送门icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P1219

// Problem: 
//     P1219 [USACO1.5] 八皇后 Checker Challenge
//   
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1219
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<map>
using namespace std;
int pluss[105];
int sub[105];
int n;
int a[15];//记录列
int k=0;
bool line[15];
#define endl '\n'

void print(){
	for(int i=1;i<=n;++i) cout<<a[i]<<' ';
	cout<<endl;
}

void dfs(int x){//第n行
	if(x==n){
		if(++k<=3) print();
		return;
	}
	
	for(int i=1;i<=n;++i){
		if(pluss[i+x+1]==0&&sub[i-x-1+n]==0&&!line[i]){
			line[i]=true;
			pluss[(i+x+1)]++;sub[i-x-1+n]++;
			a[x+1]=i;
			dfs(x+1);
			line[i]=false;//每一列查询有没有
		    pluss[(i+x+1)]--;sub[i-x-1+n]--;
		}
	}
}



int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n;
    dfs(0);
    cout<<k<<endl;
	return 0;
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值