深搜TLE60分怎么办啊啊啊啊(破防)
查看原帖
深搜TLE60分怎么办啊啊啊啊(破防)
1070708
Caged_Bird楼主2023/9/1 22:43

啊啊啊我除了深搜想不出其他办法了~~~题解又看不懂。。。。。。

#include<bits/stdc++.h>
using namespace std;
int num[20],a[20],n,ans;
bool used[20];
bool L(int t[]){
    int pre=1,top=0,i,fail=0;
    for(i=1;i<=n;i++){
        while(pre<=t[i])a[++top]=pre++;
        if(a[top]==t[i])top--;
        else fail=1;
    }
    if(fail==0)return 1;
    return 0;
}
void dfs(int step){
	if(step>n){if(L(num))ans++;return;}
	for(int i=1;i<=n;i++){
		if(!used[i]){
			used[i]=1;
			num[step]=i;
			dfs(step+1);
			used[i]=0;
		}
	}
}
int main(){
	cin>>n;
	dfs(1);
	cout<<ans;
	return 0;
}
2023/9/1 22:43
加载中...