[USACO1.5]八皇后 Checker Challenge
题目描述
一个如下的 $6 \times 6$ 的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。
上面的布局可以用序列 $2\ 4\ 6\ 1\ 3\ 5$ 来描述,第 $i$ 个数字表示在第 $i$ 行的相应位置有一个棋子,如下:
行号 $1\ 2\ 3\ 4\ 5\ 6$
列号 $2\ 4\ 6\ 1\ 3\ 5$
这只是棋子放置的一个解。请编一个程序找出所有棋子放置的解。
并把它们以上面的序列方法输出,解按字典顺序排列。
请输出前 $3$ 个解。最后一行是解的总个数。
输入格式
一行一个正整数 $n$,表示棋盘是 $n \times n$ 大小的。
输出格式
前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。
样例 #1
样例输入 #1
6
样例输出 #1
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
提示
【数据范围】
对于 $100\%$ 的数据,$6 \le n \le 13$。
题目翻译来自NOCOW。
USACO Training Section 1.5
dfs?
#include <bits/stdc++.h>
using namespace std;
int n=0;
int r[1050]= {0},c[1050]= {0},d1[1050]= {0},d2[1050]= {0};
// store row column and diagonal
int cnt=0;
void print() {
cnt++;
if(cnt<=3) {
for(int i=1; i<=n; i++) {
printf("%d ",r[i]);
}
printf("\n");
}//print the first three answer
}
int dfs(int step) {
for(int i = 1; i <= n; i++) {
if(c[i] == 0 && d1[i + step] == 0 && d2[step - i + n ] == 0 ) { //judge the conditions
r[step] = i;//row (i) column (j)
c[i] = 1;//hold column
d1[i + step] = 1;
d2[step -i + n] = 1;//hold diagonals
if(step == n) print();
else dfs(step+1);//continue
c[i]=0;
d1[i+step]=0;
d2[step - i + n]=0;//back
}
}
return 0;
}
int main () {
scanf("%d",&n);
dfs(1);
printf("%d\n",cnt);
}