参考:https://blog.csdn.net/qq_30076791/article/details/50573512
第3题:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int ans=0; 4 void dfs(int di,int hu,int num) 5 { 6 if (di==0&&hu==1&&num==1)//要注意审题!酒要喝光 7 { 8 ans++; 9 return; 10 } 11 if (di)//如果有店可遇到 12 { 13 dfs(di-1,hu,num*2); 14 } 15 if (hu&&num>0)//如果还有花可遇到且手上有酒 16 { 17 dfs(di,hu-1,num-1); 18 } 19 } 20 int main() 21 { 22 dfs(5,10,2); 23 cout<<ans; 24 25 return 0; 26 }
第4题:注意判断的正确书写
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[15]; 4 void show() 5 { 6 for (int i=1;i<=12;i++) 7 { 8 printf("[%d]",a[i]); 9 } 10 printf(" "); 11 } 12 int test() 13 { 14 int t=a[1]+a[3]+a[6]+a[8]; 15 if (t==a[1]+a[4]+a[7]+a[11]&&t==a[2]+a[3]+a[4]+a[5]&&t==a[2]+a[6]+a[9]+a[12]&&t==a[8]+a[9]+a[10]+a[11]&&t==a[5]+a[7]+a[10]+a[12]) 16 {//注意if里面要用多个&&来比较,不可用多个==来比较!! 17 return 1; 18 } 19 return 0; 20 } 21 int main() 22 { 23 for (int i=1;i<=12;i++) 24 { 25 a[i]=i; 26 } 27 do 28 { 29 // show(); 30 if (test()) 31 { 32 printf("%d ",a[6]); 33 break; 34 } 35 }while (next_permutation(a+1,a+13)); 36 37 return 0; 38 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[15]; 4 int vis[15]; 5 void look() 6 { 7 for (int i=1;i<=12;i++) 8 { 9 printf("[%d]",a[i]); 10 } 11 printf(" "); 12 } 13 int test() 14 { 15 int t=a[1]+a[3]+a[6]+a[8]; 16 if (t==a[1]+a[4]+a[7]+a[11]&&t==a[2]+a[3]+a[4]+a[5]&&t==a[2]+a[6]+a[9]+a[12]&&t==a[8]+a[9]+a[10]+a[11]&&t==a[5]+a[7]+a[10]+a[12]) 17 {//注意if里面要用多个&&来比较,不可用多个==来比较!! 18 return 1; 19 } 20 return 0; 21 } 22 void dfs(int id,int remain) 23 { 24 if (remain==0) 25 { 26 // look(); 27 if (test()) 28 printf("%d ",a[6]); 29 return; 30 } 31 for (int i=1;i<=12;i++) 32 { 33 if (vis[i]==0) 34 { 35 a[id]=i; 36 vis[i]=1; 37 dfs(id+1,remain-1); 38 vis[i]=0; 39 } 40 } 41 } 42 int main() 43 { 44 a[1]=1; 45 a[2]=8; 46 a[12]=3; 47 vis[1]=vis[8]=vis[3]=1; 48 dfs(3,9); 49 50 return 0; 51 }
第九题:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1e9+7; 4 int tu[55][55]; 5 int n,m,k,ans=0; 6 void test() 7 { 8 for (int i=1;i<=n;i++) 9 { 10 for (int j=1;j<=m;j++) 11 { 12 printf("[%d]",tu[i][j]); 13 } 14 printf(" "); 15 } 16 } 17 void read() 18 { 19 for (int i=1;i<=n;i++) 20 { 21 for (int j=1;j<=m;j++) 22 { 23 cin>>tu[i][j]; 24 } 25 } 26 } 27 void dfs(int x,int y,int have,int most) 28 { 29 if (have>k) 30 return; 31 if (x==n&&y==m)//注意跳出条件! 32 { 33 if (have==k||have==k-1&&most<tu[x][y]) 34 { 35 ans++; 36 ans=ans%N; 37 } 38 } 39 if (x+1<=n) 40 { 41 if (tu[x][y]>most) 42 { 43 swap(tu[x][y],most); 44 dfs(x+1,y,have+1,most); 45 swap(tu[x][y],most); 46 dfs(x+1,y,have,most); 47 } 48 else 49 dfs(x+1,y,have,most); 50 } 51 if (y+1<=m) 52 { 53 if (tu[x][y]>most) 54 { 55 swap(tu[x][y],most); 56 dfs(x,y+1,have+1,most); 57 swap(tu[x][y],most); 58 dfs(x,y+1,have,most); 59 } 60 else 61 dfs(x,y+1,have,most); 62 } 63 } 64 int main() 65 { 66 // freopen("in.txt","r",stdin); 67 cin>>n>>m>>k; 68 read(); 69 dfs(1,1,0,-1); 70 cout<<ans; 71 72 return 0; 73 }