Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴份c++暴搜AC代码,要找到合适的搜索方式#include <iostream> using namespace std; int comb[12][12], ans[11], N, sum; bool flag[11]; bool solve(int s, int q); int main() { for (int i = 1; i <= 11; i++) { comb[i][1] = 1; comb[i][i] = 1; } for (int i = 2; i <= 11; i++) { for (int j = 2; j < i; j++) { comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j]; } } //先打表记录组合数,由于 N 代表的是元素个数,故两个维度的下标整体加一 cin >> N >> sum; for (int i = 1; i <= N; i++) { flag[i] = true; } solve(1, 0); return 0; } bool solve(int s, int q) { if (s == N) { for (int i = 1; i <= N; i++) { if (flag[i] && ((q + i) == sum)) { ans[s] = i; cout << ans[1]; for (int j = 2; j <= N; j++) { cout << ' ' << ans[j]; } cout << '\n'; return true; } } } else { for (int i = 1; i <= N; i++) { if (flag[i]) { ans[s] = i; flag[i] = false; if (solve(s + 1, q + i * comb[N][s])) { return true; } flag[i] = true; } } } return false; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator