求助大佬,87分最后一个tle
查看原帖
求助大佬,87分最后一个tle
1275749
zcylanqiao楼主2024/3/30 11:41
/*********************************************************************

*********************************************************************/
#include <iostream>
using namespace std;

int attack[15][15], n, total = 0;
int position[15];

int dx[] = {1, -1, 0, 1};
int dy[] = {1, 1, 1, 0};

void dfs(int x, int y) {
	int xx, yy, tmp[15][15];

	if (x != 0 && y != 0) {
		for (int j = 0; j <= 3; j++) { //当前x,y可以进攻的位置
			for (int i = -1 * n; i < n; i++) {
				xx = x + i * dx[j];
				yy = y + i * dy[j];
				if ((xx >= 1 && xx <= n) && (yy >= 1 && yy <= n))
					attack[xx][yy] = 1;
			}
		}
	}

	if (y == n) {
		total++;
		if (total <= 3) {
			for (int i = 1; i <= n; i++) {
				cout << position[i] << " ";
			}
			cout << endl;
		}
		return;
	}

	for (int j = 1; j <= n; j++) { //寻找当前x,y下一行剩余的位置
		if (!attack[j][y + 1]) {
			position[y + 1] = j;
			for (int a = 1; a <= n; a++) {
				for (int b = 1; b <= n; b++)
					tmp[a][b] = attack[a][b];
			}
			dfs(j, y + 1);
			for (int a = 1; a <= n; a++) {
				for (int b = 1; b <= n; b++)
					attack[a][b] = tmp[a][b];
			}
		}
	}
}

int main() {
	cin >> n;
	dfs(0, 0);
	cout << total;

	return 0;
}

2024/3/30 11:41
加载中...