博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdoj 1016 素数环
阅读量:4216 次
发布时间:2019-05-26

本文共 689 字,大约阅读时间需要 2 分钟。

DFS1
#include
using namespace std;int n, pre;int vis[23];int p[23];int cnt;int kcase;bool isPrime(int num){ for(int i = 2; i*i <= num; ++i){ if(num%i == 0) return false; } return true;}void dfs(int cur, int pre, int s){ if(!isPrime(cur+pre)) return; if(s == n && isPrime(cur+1)){ int flag = 1; for(int i = 0; i < n; ++i){ if(flag){ cout << p[i]; flag = 0; } else{ cout <<" "<

采用数组 记录 类似八皇后处理 正确性是基于 p数组 随着深度变化 不断被改变

DFS2

void dfs(int s){	if(s == n && isPrime(p[s-1]+1)){		int flag = 1;		for(int i = 0; i < s; ++i){			if(flag){				cout << p[i]; 				flag = 0;			}			else{				cout <<" "<
第二种DFS一定注意  s++ 位置 如果采用dfs(s+1)则 当s+1 == n时候 s == n-1  此时回退到起点 s将退为0 而不是1

转载地址:http://zmimi.baihongyu.com/

你可能感兴趣的文章
【一天一道LeetCode】#58. Length of Last Word
查看>>
【一天一道LeetCode】#59. Spiral Matrix II
查看>>
【一天一道LeetCode】#30. Substring with Concatenation of All Words
查看>>
【一天一道LeetCode】#60. Permutation Sequence.
查看>>
【一天一道LeetCode】#118. Pascal's Triangle
查看>>
JAVA实现文件树
查看>>
ebay api GetMyMessages 函数
查看>>
手动12 - 安装php加速器 Zend OPcache
查看>>
set theme -yii2
查看>>
yii2 - controller
查看>>
yii2 - 增加actions
查看>>
php图像处理函数大全(缩放、剪裁、缩放、翻转、旋转、透明、锐化的实例总结)
查看>>
magento url中 uenc 一坨编码 base64
查看>>
强大的jQuery焦点图无缝滚动走马灯特效插件cxScroll
查看>>
Yii2.0 数据库查询
查看>>
yii2 db 操作
查看>>
mongodb group 有条件的过滤组合个数。
查看>>
关于mongodb的 数组分组 array group
查看>>
MongoDB新的数据统计框架介绍
查看>>
mongodb 增加全文检索索引
查看>>