Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1.…
算法:搜索 题意:相邻的两个数之和是素数,别忘了最后一个,和第一个 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number…
Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1. Inpu…
素数环(暴力)(紫书194页) Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle shoul…
(一)八皇后问题 (1)回溯 #include <iostream> #include <string> #define MAXN 100 using namespace std; int tot = 0, n = 8; int C[MAXN]; void search(int cur) { if(cur == n) ++tot; //递归边界,仅仅要走到了这里.全部皇后必定不冲突 else for(int i = 0; i < n; ++i) { int ok = 1; C…
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 40069 Accepted Submission(s): 17675 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1,…
解体心得: 1.一个回溯法,可以参考八皇后问题. 2.题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序.不用去特意排序. 3.输出有个坑,就是在输出一串的最后不能有空格,不然要PE,很尴尬. 题目: Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the…
Prime Ring Problem 思路:先看成一条链,往里头填数,满足任意相邻两数和为质数(这可以打表预处理出40以内的所有质数,扩展的时候枚举),填完了后检查首尾是否满足条件.字典序可以采用扩展时从小到大枚举.另外注意对于每个case多输出一个换行,行末不要有空格. #include<bits/stdc++.h> using namespace std; int n,cnt,a[25]; bool p[45],vis[25]; void print() { for(int i=1;i&l…