您的位置:首页 > 其它

USACO6.5.5 Checker Challenge( checker)

2015-02-06 19:51 218 查看
USACO最后一题,八皇后的类似问题,dfs加优化就可以轻松过了

/*
ID:xsy97051
TASK:checker
LANG:C++
*/
#include <iostream>
#include <fstream>

int n, aim, ans;
int a[20];

void dfs(int row, int ld, int rd, int i)
{
int p, pos;
if(row!=aim)
{
pos=aim&(~(row|ld|rd));
while(pos!=0)
{
p=pos&(pos^(pos-1));
pos-=p;
if(ans<3)
{
int l=1, k=p;
while(k!=1)
{
k=k>>1;
l++;
}
a[i]=l;
}
dfs(row|p,(ld|p)<<1,(rd|p)>>1,i+1);
}
}
else
{
ans++;
if(ans<=3)
{
cout<<a[1];
for(int k=2; k<=n; k++)
cout<<" "<<a[k];
cout<<endl;
}
}
}

int main()
{
freopen("checker.in","r",stdin);
freopen("checker.out","w",stdout);
cin>>n;
aim=(1<<n)-1;
dfs(0,0,0,1);
cout<<ans<<endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: