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

38 lines
1.0 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;
int n, m, Max;
int b1[N]; //正对角线
int b2[N]; //反对角线
//就是加一个常数增量将x-y与y-x转化为正数当然大于10的数字也可以但对应的数组长度要装的下才行。
const int M = 30;
void dfs(int x, int y, int sum) {
if (x == n + 1) {
//收集最大值
Max = max(Max, sum);
return;
}
//如果这个位置可以放,那么我们可以选择放
if (!b1[x + y] && !b2[x - y + M]) {
b1[x + y] = 1, b2[x - y + M] = 1;
if (y == m)
dfs(x + 1, 1, sum + 1);
else
dfs(x, y + 1, sum + 1);
b1[x + y] = 0, b2[x - y + M] = 0;
}
//不管可以不可以放,都可以选择不放
if (y == m)dfs(x + 1, 1, sum);
else dfs(x, y + 1, sum);
}
int main() {
cin >> n >> m; // n行m列
dfs(1, 1, 0); //从(1,1)位置出发,0:表示已经安排好的传教士数量
cout << Max << endl;
return 0;
}