http://train.usaco.org/usacoprob2?a=BI4LEPNXcEa&S=checker
题目大意:在一个N*N的棋盘上放棋子,要求每一行每一列都有一个,每一个对角线不超过一个(不仅仅主对角线),要求输出前三个答案(每一行的棋子所在的列号)以及摆放方法总数
USACO最后一题,再加上题目里宣扬的种种紧张氛围,还给了整整五个提示,我还以为会是一道巨坑,没想到就是简单的回溯,果然是用来检查诚信度的,搞得我都想试试看打表的后果了(笑
/*
ID: frontie1
TASK: checker
LANG: C++
*/
#include <iostream>
#include <cstdio>
using namespace std;
int N;
bool col_fill[15];
bool updia_fill[30];
bool dodia_fill[30];
int answer[15];
int cnt = 0;
void place_row(int r)
{
if(r == N+1){
++cnt;
if(cnt <= 3){
printf("%d", answer[1]);
for(int i = 2; i <= N; ++i){
printf(" %d", answer[i]);
}
printf("\n");
}
}
for(int c = 1; c <= N; ++c){
if(!col_fill[c] && !updia_fill[r-c+N] && !dodia_fill[r+c]){
col_fill[c] = updia_fill[r-c+N] = dodia_fill[r+c] = true;
answer[r] = c;
place_row(r+1);
col_fill[c] = updia_fill[r-c+N] = dodia_fill[r+c] = false;
}
}
}
int main()
{
freopen("checker.in", "r", stdin);
freopen("checker.out", "w", stdout);
scanf("%d", &N);
place_row(1);
printf("%d\n", cnt);
return 0;
}