【搜索】HDU1016
来源:互联网 发布:.exe软件脱壳工具 编辑:程序博客网 时间:2024/06/05 18:33
DFS搜索,实现做预处理可以加快速度。
1. 由于0<n<20,可以先算出3~19+18的所有数是不是质数。
2. 可以事先算出每个数可以与哪些数组合,直接使用减少for循环次数
JAVA版当输出数据量大时不要使用System.out,使用PrintWriter。
以前用System.out一直4000MS TLE,用PrintWriter是600MS
import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.util.Scanner;public class Main {int N;boolean[] appeard = new boolean[20];int[] path = new int[20];boolean[] isprime = new boolean[38];int[][] table = new int[20][20];PrintWriter out;public boolean isPrime(int num) {int sqrt = (int) Math.floor(Math.sqrt(num));for (int i = 2; i <= sqrt; i++) {if (num % i == 0) {return false;}}return true;}public static void main(String[] args) throws IOException {new Main().work();}public void work() throws IOException {Scanner input = new Scanner(System.in);out = new PrintWriter(new OutputStreamWriter(System.out));for (int i = 3; i < isprime.length; i++) {isprime[i] = isPrime(i);}int index;for (int i = 1; i < 20; i++) {index = 0;for (int j = 1; j < 20; j++) {if (i == j) {continue;}if (isprime[i + j]) {table[i][index++] = j;}}}appeard[1] = true;path[1] = 1;int counter = 1;while (input.hasNext()) {N = input.nextInt();input.nextLine();out.println("Case " + counter++ + ":");dfs1(1);out.println();out.flush();}input.close();}public void dfs1(int alreadyAppeardCount) throws IOException {int previous = path[alreadyAppeardCount];int next;if (alreadyAppeardCount == N) {if (isprime[previous + 1]) {//*//for (int i = 1; i <= N; i++) {out.print(path[i]);if (i != N) {out.print(" ");} else {out.println();}}}}for (int i = 0; i <= N; i++) {next = table[previous][i];if (next == 0) {return;}if (next > N) {continue;}if (appeard[next]) {continue;}appeard[next] = true;//*//path[alreadyAppeardCount + 1] = next;dfs1(alreadyAppeardCount + 1);//*//appeard[next] = false;//*//}}}
- 【搜索】HDU1016
- hdu1016
- hdu1016
- hdu1016
- hdu1016
- hdu1016
- hdu1016
- hdu1016
- hdu1016
- hdu1016
- hdu1016
- HDU1016
- HDU1016
- hdu1016
- hdu1016
- HDU1016
- hdu1016
- hdu1016
- JavaWeb开发入门
- ARM9 2410移植之ARM中断原理, 中断嵌套的误区,中断号的怎么来的
- javax.servlet.Filter过滤器用法
- Oracle SQL的优化
- 基本数据类型分析
- 【搜索】HDU1016
- UINavigationController
- android下opengles2.0多线程问题
- 带aidl文件的应用程序在android平台源码中的编译
- poj 1019(数学题)
- Android-- Toast通知的用法
- 关于安妮宝贝
- android和iOS平台的崩溃捕获和收集
- 纯代码增强wordpress评论编辑器实现淡入淡出效果