dfs ZOJ 1711 Sum It Up
2010-09-10 19:38
399 查看
这几天学,BFS和DFS算法,参照网上找的程序,对照着写的。
所有的排列组合问题,基本是基于DFS算法的思路而来的
Sum It Up
所有的排列组合问题,基本是基于DFS算法的思路而来的
Sum It Up
#include<iostream> using namespace std; int t; int n; int num[12]; int rcd[12]; int usedtimes[12]; int differentnums; bool isfind = false; void norepeat_combination(int, int); void print(int); int main() { int count = 0; int a; bool flag; //freopen("test.txt","r",stdin); while (cin >> t >> n && !(t==0 && n == 0) ) { for (int i = 0; i < n; i++) { usedtimes[i] = 1; } flag = true; for (int i = 0; i < n; i++) { cin >> a; for (int j = 0; j < count; j++ ) { if (a == num[j]) { flag = false; usedtimes[j]++; } } if (flag) { num[count] = a; count++; } flag = true; } n = count; cout << "Sums of " << t << ":" << endl; norepeat_combination(0, 0); if (!isfind) { cout << "NONE" << endl; } isfind = false; count = 0; } return 0; } int sum; //生成不重复组合,类似DFS过程 void norepeat_combination(int len, int current) { sum = 0; for (int i = 0; i < len; i++) { sum += rcd[i]; } if (sum == t) { isfind = true; print(len); } else { for (int i = current; i < n; i++) { if (usedtimes[i]) { usedtimes[i]--; rcd[len] = num[i]; norepeat_combination(len+1, i);//这里不是i+1,要把所有重复的都用完,才填入下一个不同的数字 usedtimes[i]++; } } } } void print(int len) { for (int i = 0; i < len; i++) { cout << rcd[i]; if (i != len - 1) { cout << "+"; } } cout << endl; }
相关文章推荐
- ZOJ 1711 Sum It Up(DFS啊 )
- poj1564 Sum It Up (zoj 1711 hdu 1258) DFS
- poj 1564 && zoj 1711 Sum It Up (dfs)
- poj1564 Sum It Up (zoj 1711 hdu 1258) DFS
- zoj 1711 || poj 1564 Sum It Up(DFS~~~去重~)
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
- ZOJ1711 POJ1564 Sum It Up,DFS+输出排序+去重复
- zoj 1711 Sum It Up
- ZOJ 1711 Sum It Up
- ZOJ 1711 Sum It Up
- zoj 1711 Sum It Up
- zoj1711-Sum It Up
- POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
- ZOJ 1711 Sum It Up
- hdu 1258和ZJU 1711 Sum It Up(dfs经典索搜)
- HDU 1258 Sum It Up(DFS)
- POJ 1564 && HDU 1258 Sum It Up(dfs)
- hdu1258 Sum It Up (DFS)
- POJ 1564 Sum It Up -dfs(回溯法)