题解:http://www.matrix67.com/blog/archives/266
#include <cstdio> int n,ans,upperlim; void test(int row,int ld,int rd) { int pos,p; if (row!=upperlim) { pos=upperlim&(~(row|ld|rd)); while(pos) { p=pos&(~pos+1); pos=pos-p; test(row|p,(ld|p)<<1,(rd|p)>>1); } } else ans++; } int main() { int i; while(scanf("%d",&n)&&n) { upperlim=(1<<n)-1; ans=0; test(0,0,0); printf("%d ",ans); } return 0; }