为啥运行错误?
  • 板块学术版
  • 楼主Suare_Pi
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/2/12 11:04
  • 上次更新2023/10/24 01:01:10
查看原帖
为啥运行错误?
756563
Suare_Pi楼主2023/2/12 11:04

1213:八皇后问题

#include <bits/stdc++.h>
using namespace std;
int a[20],b[20],c[20],d[20];
int num = 0;
void ans() {
   cout << "NO." << " " << ++num << endl;
	for (int i = 1; i <= 8; i++) {
		for (int j = 1; j <= 8; j++) {
			if (a[j] == i) {
				cout << "1" << " ";
			}
			else{
				cout << "0" << " ";
			}
		}
		cout << endl;
	}
}

int dfs(int x) {

	for (int i = 1; i <= 8; i++) {
		if (b[i] && c[x + i - 1] && d[x - i + 8]) {
			a[x] = i;
			b[i] = 0;
			c[x + i - 1] = 0;
			d[x - i + 8] = 0;
			if (x == 8)
				ans();
			else
				dfs(x + 1);
			b[i] = 1;
			c[x + i - 1] = 1;
			d[x - i + 8] = 1;
		}
	}
}

int main() {
	memset(b, 1, sizeof(b));
	memset(c, 1, sizeof(c));
	memset(d, 1, sizeof(d));
	dfs(1);
	return 0;
}
  
2023/2/12 11:04
加载中...