标签:const else bool eof bsp print spl continue color
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 5454 | Accepted: 2923 | Special Judge |
5 7 12 15 77 0
1 2 4 5 1 2 4 6 7 1 2 4 8 12 1 2 4 5 10 15 1 2 4 8 9 17 34 68 77
//By zZhBr #include <iostream> #include <cstdio> #include <cstring> using namespace std; int n; int ans; int a[1100]; bool use[1005]; bool DFS(int stp) { memset(use, 0, sizeof use); if(stp > ans) { if(a[ans] == n) return 1; else return 0; } for(register int i = stp - 1 ; i >= 1 ; i --) { for(register int j = i ; j >= 1 ; j --) { if(a[i] + a[j] > n) continue; if(!use[a[i] + a[j]]) { if(a[i] + a[j] <= a[stp - 1]) return 0; use[a[i] + a[j]] = 1; a[stp] = a[i] + a[j]; if(DFS(stp + 1)) return 1; a[stp] = 0; use[a[i] + a[j]] = 0; } } } } int main() { while(scanf("%d", &n) != EOF) { if(n == 0) return 0; if(n == 1) { printf("1\n"); continue; } if(n == 2) { printf("1 2\n"); continue; } a[1] = 1;a[2] = 2; for(ans = 3 ; !DFS(3) ; ans ++); for(register int i = 1 ; i <= ans ; i ++) { printf("%d ", a[i]); } printf("\n"); memset(a, 0, sizeof a); } return 0; }
poj2248 Addition Chains 迭代加深搜索
标签:const else bool eof bsp print spl continue color
原文地址:https://www.cnblogs.com/zZh-Brim/p/8977282.html