hdu2510-符号三角形(dfs+打表)
n只有24 可以写个暴力搜索,然后打表,不然这个很难通过剪枝直接优化到1s以内。
1 #include<bits/stdc++.h> 2 3 #define inf 0x3f3f3f3f 4 5 const int maxn=100; 6 7 using namespace std; 8 9 int n,m,ans; 10 11 int res[maxn+10]={0,0,0,4,6,0,0,12,40,0,0,171,410,0,0,1896,5160,0,0,32757,59984,0,0,431095,822229}; 12 13 int a[maxn+10][maxn+10]; 14 15 void dfs(int layer,int lnum,int pos,int nei,int n){ 16 //printf("%d %d %d %d\n",layer,lnum,pos,nei); 17 if(pos+nei>m) return ; 18 if(pos-nei>(m-pos-nei)) { 19 //printf("%d %d\n",pos,nei); 20 return ; 21 } 22 if(layer==0&&pos==m/2&&nei==m/2){ 23 ans++; 24 return ; 25 } 26 if(layer==0&&(pos!=m/2||nei!=m/2)) return ; 27 if(pos>m/2||nei>m/2) return ; 28 if(layer==n&&lnum<=layer){ 29 a[layer][lnum]=1; 30 if(lnum==layer) 31 dfs(layer-1,1,pos+1,nei,n); 32 else dfs(layer,lnum+1,pos+1,nei,n); 33 a[layer][lnum]=-1; 34 if(lnum==layer) 35 dfs(layer-1,1,pos,nei+1,n); 36 else dfs(layer,lnum+1,pos,nei+1,n); 37 } else if(layer!=n){ 38 if(lnum<=layer){ 39 if(a[layer+1][lnum]==a[layer+1][lnum+1]){ 40 a[layer][lnum]=1; 41 if(lnum==layer) 42 dfs(layer-1,1,pos+1,nei,n); 43 else dfs(layer,lnum+1,pos+1,nei,n); 44 } else { 45 a[layer][lnum]=-1; 46 if(lnum==layer) 47 dfs(layer-1,1,pos,nei+1,n); 48 else dfs(layer,lnum+1,pos,nei+1,n); 49 50 } 51 } 52 53 } 54 } 55 56 int main() 57 { 58 /*for(int i=1;i<=24;i++){ 59 m=(i*(i+1)/2); 60 if(m&1){ 61 printf("0\n"); 62 } else { 63 ans=0; 64 dfs(i,1,0,0,i); 65 printf("%d\n",ans); 66 } 67 }*/ 68 while(scanf("%d",&n)!=EOF&&n){ 69 printf("%d %d\n",n,res[n]); 70 } 71 return 0; 72 }