您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: