赞
踩
2 1
4
3 1
1
- #include <iostream>
- using namespace std;
-
- int n, m, tot;
- int *row, *column, *slash, *reslash;
-
- int judge() {
- for (int i = 1; i <= n; i++) {
- if (row[i] == 999) {
- for (int j = 1; j <= n; j++) {
- if (column[j] == 999) {
- if (!(reslash[i + j] == 1 || slash[n + i - j + 1] == 1)) {
- return 0;
- }
- }
- }
- }
- }
- return 1;
- }
-
- void traceback(bool flag, int level, int num) {
- if (level > n) {
- if (num != m) {
- return;
- }
- if (judge()) {
- tot++;
- }
- return;
- }
-
- if (n - level + num < m) {
- return;
- }
-
- if (!flag) {
- traceback(false, level + 1, num);
- traceback(true, level + 1, num + 1);
- } else {
- for (int i = 1; i <= n; i++) {
- if (column[i] == 999 && reslash[level + i] == 999 && slash[n + level - i + 1] == 999) {
- row[level] = level;
- column[i] = i;
- reslash[level + i] = 1;
- slash[n + level - i + 1] = 1;
- traceback(false, level + 1, num);
- traceback(true, level + 1, num + 1);
- row[level] = 999;
- column[i] = 999;
- reslash[level + i] = 999;
- slash[n + level - i + 1] = 999;
- }
- }
- }
- }
-
- int main() {
- cin >> n >> m;
- column = new int[n + 1];
- row = new int[n + 1];
- slash = new int[2 * n + 1];
- reslash = new int[2 * n + 1];
-
- for (int i = 0; i <= n; i++) {
- column[i] = row[i] = 999;
- }
-
- for (int i = 0; i < 2 * n + 1; i++) {
- slash[i] = reslash[i] = 999;
- }
-
- tot = 0;
- traceback(false, 1, 0);
- traceback(true, 1, 1);
- cout << tot << endl;
-
- delete[] column;
- delete[] row;
- delete[] slash;
- delete[] reslash;
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。