0分求助
查看原帖
0分求助
464148
xiaoxiaodai楼主2022/10/16 19:05

为什么连样例都过不去

#include <bits/stdc++.h>
using namespace std;

int n,a[101],b[101],c[101],d[101],t;

void print() {
	if( t <= 2 ) {
		for( int i=1; i<=n; ++i ) {
			cout << a[i] << " ";
		}
		cout << endl;
	}
	++t;
	return;
}

void dfs( int q ){
	if( q == n ) {
		print();
		return;
	}
	else {
		for( int i=1; i<=n; ++i ) {
			if( b[i] == 0 && c[i+q] == 0 && d[q-i+n] == 0 ) {
				a[q] = i;
				b[i] = 1;
				c[i+q] = 1;
				d[q-i+n] = 1;
				dfs(q+1);
				b[i] = 0;
				c[i+q] = 0;
				d[q-i+n] = 0;
			}
		}
	}
}

int main() {

	cin >> n;
	
	dfs( 1 );
	
	cout << t;
	
	return 0;
}
2022/10/16 19:05
加载中...