【搜索】数学组の问题
There are three girls and fifteen boys who are having a meeting at a round rable.You need to put them at proper place to make sure that "there are least four boys between every two girls".You have to print the answer that how many different ways can you find.And the ways themself.Attention:the ways that can be rotate to the same is the same.
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int ans,p,en; bool a[19]; 5 bool save[10001][19]; 6 bool check() 7 { 8 for(int i=1;i<=18;i++) 9 if(a[i]) 10 { 11 for(int j=i-1;j>=i-4;j--) if(a[j]) return 0; 12 for(int j=18;j>=14+i;j--) if(a[j]) return 0; 13 for(int j=i+1;j<=i+4;j++) if(a[j]) return 0; 14 for(int j=1;j<=i-14;j++) if(a[j]) return 0; 15 } return 1; 16 } 17 bool panchong() 18 { 19 for(int i=1;i<=en;i++) 20 { 21 for(int j=1;j<=18;j++)//枚举起点 22 { 23 int l=1; 24 for(int k=j;k<=18;k++,l++) if(a[k]!=save[i][l]) goto OUT; 25 for(int k=1;k<j;k++,l++) if(a[k]!=save[i][l]) goto OUT; 26 return 0; 27 OUT:; 28 } 29 } 30 return 1; 31 } 32 void dfs(int man,int woman) 33 { 34 if(man<0||woman<0) return; 35 if(man==0&&woman==0) 36 { 37 if(check()&&panchong()) 38 { 39 ans++; 40 memcpy(save[++en],a,sizeof(a)); 41 } 42 return; 43 } 44 a[++p]=1; 45 dfs(man,woman-1);//1:nv 46 a[p]=0; 47 dfs(man-1,woman);//0:nan 48 p--; 49 } 50 int main() 51 { 52 dfs(15,3); 53 for(int i=1;i<=en;i++) 54 { 55 for(int j=1;j<=18;j++) printf("%d ",save[i][j]); 56 puts(""); 57 } 58 printf("%d\n",ans); 59 return 0; 60 }
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/