p1016 深搜
来源:互联网 发布:南平seo自动优化软件 编辑:程序博客网 时间:2024/06/01 10:53
package cn.hncu.search;import java.util.Scanner;public class Seach3 {public static boolean isPrime(int n){if(n==2)return true;for(int i=2;i*i<=n;i++){if(n%i==0){return false;}}return true;}public static void main(String[] args) {Scanner sc = new Scanner(System.in);while(sc.hasNext()){int n = sc.nextInt();//初始化图Node nodes[] = new Node[n];for(int i=0;i<nodes.length;i++){nodes[i] = new Node(i+1,-1,null);}int startNum=0;int count=0;//计数dfs(nodes,startNum,count);}}private static void dfs(Node[] nodes, int u, int count) {count++;nodes[u].color=1;//鸿沟if(count==nodes.length && isPrime(nodes[u].value+nodes[0].value) ){print(nodes,u);}for(int v=1;v<nodes.length;v++){if(nodes[v].color==-1 && isPrime(nodes[u].value+nodes[v].value)){nodes[v].parent = nodes[u];dfs(nodes,v,count);nodes[v].color=-1;nodes[v].parent=null;}}}private static void print(Node[] nodes, int u) {//倒输出所有的值Node nodes2[] = new Node[nodes.length];Node node = nodes[u];for(int i=nodes.length-1; i>=0; i--){nodes2[i] = node;node = node.parent;}int i=0;for(;i<nodes.length-1;i++){System.out.print(nodes2[i].value+" ");}System.out.println(nodes2[i].value);}}class Node{int value;int color;Node parent;public Node(int value, int color, Node parent) {this.value = value;this.color = color;this.parent = parent;}}
0 0
- p1016 深搜
- P1016
- HDOJ P1016 Prime Ring Problem
- Vijos P1016北京2008的挂钟
- 【vijos】P1016北京2008的挂钟
- Tyvj P1016 装箱问题 (DP)
- Vijos P1016 钟表问题 BFS+剪枝
- |Tyvj|动态规划|P1016 装箱问题
- 洛谷 P1016 旅行家的预算
- vijos P1016 北京2008的挂钟(poj 1166)
- 洛谷 P1016 [NOIP1999 T2] 旅行家的预算
- 贪心——洛谷P1016 旅行家的预算
- 【Luogu P1048 Luogu P1016】采药/疯狂的采药
- 深搜
- 深搜
- 深搜
- 深搜
- 深搜
- 修改myeclipse的jsp模板
- android:persistentDrawingCache含义
- distribute_crawler项目实战
- jQuery 效果 - slideToggle() 方法
- 上机编程题-约瑟夫问题(升级版)
- p1016 深搜
- 前端的Project示例
- Install Scala and SBT using apt-get on Ubuntu 14.04 or any Debian derivative using apt-get
- Java成员变量的访问权限
- Alipay秘钥校验工具
- 在SDK中使用GDI+出现的编译问题解决方法
- 初识Halcon
- IOS本地化操作
- 【十四】记录