求助
查看原帖
求助
119537
mirayan楼主2021/3/11 01:24

求助,萌新第一次写搜索,第二次写递归qwq; 思路是一行行遍历,然后如果有合适的位置就标记所在纵行(b),2斜行(c1、c2),并进入下一行(k是行数),没有则返回。 目前问题是无论输几最后输出都是0,QWQ

#include <bits/stdc++.h>
using namespace std;
int n, result, b[100005] = {0}, a[100005] = {0}, c1[100005] = {0}, c2[100005] = {0}, ans = 0, jud = 0;
int i;

void dfs(int k) {
	while (k < n) {
		jud = 0;

		for (i = 0; i < n; i++) {

			if (b[i] > 0) {
				jud = 1;
			}

			if (c1[4 - k + i] > 0) {
				jud = 1;
			}

			if (c2[k + i] > 0) {
				jud = 1;
			}

			if (jud == 0) {

				a[k] = i;
				b[i] = 1;
				c1[4 - k + i] = 1;
				c2[k + i] = 1;
				k++;
				dfs(k);
				k--;
				a[k] = 0;
				b[i] = 0;
				c1[4 - k + i] = 0;
				c2[k + i] = 0;
				return;
			}
		}
		k++;
	}



	if (k == n - 1) {
		ans++;

		if (ans < 3) {
			for (int j = 0; j < n; j++)

				cout << a[j] << " ";
		}

		cout << endl;
	}

	return;
}

int main() {

	cin >> n;
	dfs(0);
	cout << ans;
	return 0;

}
2021/3/11 01:24
加载中...