我设的st数组如果太小会令最终的答案变小,求原因
查看原帖
我设的st数组如果太小会令最终的答案变小,求原因
1263248
Zhuajie楼主2024/6/11 11:48
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
int g[14][14];//用于存放最后答案
int st[100][100];//用于存放数组状态 
int cnt = 0;//用于存放答案个数
int n = 0;//用于输入棋盘大小
int a = 3;
void dfs(int x, int y)
{
	if (x > n) {
		cnt++;
		while (a) {
			for (int i = 1; i <= n; i++) printf("%d ", g[0][i]);
			printf("\n");
			a--;
			break;
		}
		return;
	}
	int flag = 0;//flag 用来代表这一行里面还有没有可以放1的位置,如果没了flag=0,如果有flag为1.
	for (int i = 1; i <= n; i++)  if (st[x][i] == 0) flag = 1;
	if (flag == 0) return;
	for (int i = 1; i <= n; i++) {//用于遍历从左到右放棋子 ,棋子的列位置 
		if (!st[x][i]) {
			g[0][x] = i;//cnt代表第几组数,x代表第几行,i则是表示什么数 
			for (int j = 1; j <= n; j++) {//用于把其他冲突位置都变1,但不知道有负数会不会不行 
				st[x][j]++;
				st[j][i]++;
				st[j][x + i - j]++;
				st[j][i + j - x]++;
			}
			dfs(x + 1, i);
			g[0][x] = 0;
			for (int j = 1; j <= n; j++) {//用于把其他冲突位置都变1,但不知道有负数会不会不行 
				st[x][j]--;
				st[j][i]--;
				st[j][x + i - j]--;
				st[j][i + j - x]--;
			}
		}
	}
}
int main()
{
	scanf("%d", &n);
	dfs(1, 1);
	printf("%d\n", cnt);
	return 0;
}
2024/6/11 11:48
加载中...