UVa 524 - Prime Ring Problem(回溯)
2015-03-13 17:59
323 查看
回溯法的简单应用。
#include<iostream> #include<cstdio> #include<string> #include<algorithm> #define MAXN 50 using namespace std; int n, A[MAXN] = {1}, ispe[MAXN], vis[MAXN]; void dfs(int cur) { if(cur == n&& ispe[A[0] + A[n - 1]]) { for(int i = 0; i < n; i++) { i ? printf(" %d", A[i]) : printf("%d", A[i]); } printf("\n"); } else for(int i = 2; i <= n; i++) { if(!vis[i]&& ispe[i + A[cur - 1]]) { A[cur] = i; vis[i] = 1; dfs(cur + 1); vis[i] = 0; } } } int main() { for(int i = 2; i <= 50; i++) ispe[i] = 1; for(int i = 2; i <= 50; i++) for(int j = i + i; j + i <= 50; j += i) ispe[j] = 0; int kase = 0; while(cin >> n) { if(kase++) printf("\n"); printf("Case %d:\n", kase); dfs(1); } return 0; }
相关文章推荐
- UVa 524 Prime Ring Problem(DFS , 回溯)
- UVa 524 Prime Ring Problem【回溯】
- UVA - 524 Prime Ring Problem (经典回溯问题)
- Prime Ring Problem UVA - 524 回溯
- UVa 524 Prime Ring Problem (回溯)
- UVa 524 - Prime Ring Problem(回溯)
- UVa 524 Prime Ring Problem(DFS , 回溯)
- UVa 524:Prime Ring Problem(回溯)
- UVA 524 - Prime Ring Problem
- UVA - 524 Prime Ring Problem
- UVa-524 - Prime Ring Problem
- Prime Ring Problem(UVA 524)
- UVa-524 - Prime Ring Problem
- UVA - 524 Prime Ring Problem
- UVA524- Prime Ring Problem
- UVA_524_Prime Ring Problem
- UVa 524 - Prime Ring Problem
- uva 524 - Prime Ring Problem
- uva 524 prime ring problem——yhx
- UVa 524 Prime Ring Problem (数论&DFS)