#include <stdio.h>
#include <stdlib.h>
#define m 3
#define n 3
int count=0;
int a[m][n];
int ok(int x, int y) {
for (int i = 0; i < x; i++) {
if (a[i][y] == a[x][y])
return 0;
}
return 1;
}
/*考虑边界条件, 当走到最后一个元素时,说明摆放成功. 否则继续摆放.
*当走到最后一列时,记得将y至0
* */
void traceback(int x, int y) {
int temp;
for (int i = y; i < n; i++) {
temp = a[x][y];
a[x][y] = a[x][i];
a[x][i] = temp;
if (ok(x, y) == 1) {
if (x == m-1) {
if (y == n-1) {
count++;
return;
} else {
traceback(x, y+1);
}
} else {
if (y == n-1) {
traceback(x+1, 0);
} else {
traceback(x, y+1);
}
}
}
}
}
int main() {
/*将每一行赋成1---n*/
for (int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
a[i][j] = j+1;
}
}
traceback(0,0);
printf("%d", count);
}
拉丁矩阵问题
原文作者:拉丁方阵问题
原文地址: https://blog.csdn.net/x_zhaohu/article/details/53689117
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/x_zhaohu/article/details/53689117
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。