排列问题

code

#include<iostream>
using namespace std;
int a[11][11], b[11], bj[11] = {0}, n, k, sum = 0;
int f(int s) {
	int i1;
	if (s == n) {
		sum++;
		if (sum == k) {
			for (i1 = 0; i1 < n; i1++) {
				cout << b[i1] << ' ';
			}
			cout << '\n';
			return 0;
		}
	} else {
		for (i1 = 0; i1 < n; i1++) {
			if (bj[i1] == 0 && a[b[s - 1]][i1] == 1 || s == 0) {
				b[s] = i1, bj[i1] = 1;
				f(s + 1);
				bj[i1] = 0;
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	int j, i;
	cin >> n >> k;
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			cin >> a[i][j];
		}
	}
	f(0);
	return 0;
}

thought

矩阵中0位置的左不能再上后面,例子:
0 1 1
1 0 0
0 1 0
这里有效0为(2,1),(3,3),所以2不能出现在1后面,0不能出现在2后面
且i行i列为0,则所有0位的i下标行列位为0

posted @ 2022-02-03 11:40  ethon-wang  阅读(39)  评论(0编辑  收藏  举报