Files
python/TangDou/XiTi/1315_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

48 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int M = 30; //偏移量
int n, m; // n行m列
int ans[N][N];
int a[N]; //正对角线
int b[N]; //反对角线
void dfs(int x, int y, int sum) {
if (x == n + 1) {
ans[n][m] = max(ans[n][m], sum);
return;
}
//如果这个位置可以放,那么我们可以选择放
if (!a[x + y] && !b[x - y + M]) {
a[x + y] = 1;
b[x - y + M] = 1;
if (y == m)
dfs(x + 1, 1, sum + 1);
else
dfs(x, y + 1, sum + 1);
a[x + y] = 0;
b[x - y + M] = 0;
}
//不管可以不可以放,都可以选择不放
if (y == m)
dfs(x + 1, 1, sum);
else
dfs(x, y + 1, sum);
}
int main() {
//模拟7行7列从(1,1)出发,开始打表,然后找规律
for (n = 1; n <= 7; n++)
for (m = 1; m <= 7; m++)
dfs(1, 1, 0);
//输出每个行、列情况下的摆法数量
for (int i = 1; i <= 7; i++) {
for (int j = 1; j <= 7; j++)
printf("%02d ", ans[i][j]);
puts("");
}
return 0;
}