标签:io os ar for sp c r ef size
给出杨辉三角的顶点值,求底边各个数的值。直接DFS就好了
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cmath> #define ll __int64 #define INF 0x3fffffff #define rep(i,n) for(int (i)=0;(i)<n;(i)++) using namespace std; int n,k,sum; int a[15][15]; bool vis[12]; bool judge() { for(int i=1;i<n;i++) { for(int j=0;j<n-i;j++) { a[i][j]=a[i-1][j]+a[i-1][j+1]; } } if(a[n-1][0]==sum) return true; return false; } bool dfs(int cur) { if(cur==n) { if(judge()) return true; return false; } for(int i=1;i<=n;i++) { if(!vis[i]) { a[0][cur]=i; vis[i]=true; if(dfs(cur+1)) return true; a[0][cur]=0; vis[i]=false; } } return false; } int main() { cin>>n>>sum; k=0; memset(a,0,sizeof(a)); memset(vis,false,sizeof(vis)); dfs(0); rep(i,n) { cout<<a[0][i]; if(i==n-1) break; cout<<" "; } cout<<endl; return 0; }
标签:io os ar for sp c r ef size
原文地址:http://blog.csdn.net/u012198382/article/details/39909903