hdu 1016 很水的dfs
2011-07-19 13:58
369 查看
就一全奇偶剪枝罢了
#include<iostream> #include<cmath> using namespace std; int n; int t; bool mark[25]; int num[25]; void ints(){ for(int i = 0;i <= n;i++){ mark[i] = true; num[i] = 0; } num[1] = 1; mark[1] = false; } bool judge(int x){ int i; for(i = 2; i <= (int)sqrt(1.0*x); i++){ if(x%i == 0) return false; } return true; } void print(){ for(int i = 1; i < n;i++) cout <<num[i] <<' '; cout <<num <<endl; } void dfs(int step){ if(step == n+1 && judge(num[step-1]+num[1])){ print(); } if(!(step%2)){ for(int j = 2; j <= n; j += 2){ if( mark[j] && judge(j+num[step-1])){ num[step] = j; mark[j] = false; dfs(step+1); mark[j] = true; } } } else{ for(int k = 3; k <= n;k += 2){ if(judge(k+num[step-1]) && mark[k]){ num[step] = k; mark[k] = false; dfs(step+1); mark[k] = true; } } } } int main() { t = 0; while(cin >>n){ cout <<"Case " << ++t << ":" <<endl; ints(); dfs(2); cout <<endl; } return 0; }
相关文章推荐
- Hdu-1016 Prime Ring Problem【DFS】
- HDU 1016 Prime Ring Problem(DFS入门)
- DFS基础 (HDU 1010、HDU 1016、HDU 1072)
- Prime Ring Problem hdu 1016 dfs
- 【dfs】hdu 1016 素数环
- HDU 1016 Prime Ring Problem(DFS)
- HDU 1016 Prime Ring Problem (DFS回溯)
- HDU 1016 Prime Ring Problem(DFS)
- HDU 1016 Prime Ring Problem DFS + 记忆化搜索
- hdu 1016 Prime Ring Problem (dfs)
- hdu 1016 dfs+方案输出
- HDU 1016(Prime Ring Problem)DFS
- [hdu 1016] Prime Ring Problem(剪枝dfs)
- HDU-1016-Prime Ring Problem( C && 经典DFS题 )
- HDU 1016 Prime Ring Problem(dfs)
- hdu 1016 Prime Ring Problem(dfs)
- HDU 1016 Prime Ring Problem --- 经典DFS
- hdu 1016 Prime Ring Problem(dfs,素数环)
- hdu--1016 素素环(dfs)
- HDU 1016--Prime Ring Problem【DFS】