USACO:Checker Challenge

深搜一下 稍微优化一下就ok

/*
ID: Jang Lawrence
PROG: checker
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
bool is[14];
bool is2[30],is3[30];
int n,ans;
int c[14];
bool f(int &x,int &y)
{
    if(is[y]) return false;
    int k=y-(x-1)+1+n;
     if(is2[x+y]||is3[k]) return false;
    return 1;
}
void dfs(int row)
{
    if(row==n+1)
    {
        ans++;
        if(ans<=3)
        {
         for(int i=1;i<n;++i)
         printf("%d ",c[i]);
         printf("%d\n",c[n]);
        }
        return ;
    }
    for(int i=1;i<=n;++i)
    if(f(row,i))
    {c[row]=i;is[i]=1;is2[row+i]=1;
    int k=i-(row-1)+1+n;
    is3[k]=1;
    dfs(row+1);is[i]=0;is2[row+i]=0;
    is3[k]=0;}
}
int main()
{
  #ifndef  DEBUG
  freopen("checker.in","r",stdin);
  freopen("checker.out","w",stdout);
  #endif
  scanf("%d",&n);
  dfs(1);
  printf("%d\n",ans);
    return 0;
}


你可能感兴趣的:(USACO:Checker Challenge)