Prime Ring Problem(dfs+回溯)(参照全排列)

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 of first circle should always be 1.

 

Input

n (0 < n < 20).

Output

The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.

You are to write a program that completes above process.

Print a blank line after each case.

 

Sample Input

6
8
 

Sample Output

Case 1:
1 4 3 2 5 6
1 6 5 2 3 4

Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2

给你一个n  可选择的范围是 1 ~n  需要你填充一个圆环

给你一个环,输入n

第1个 + 第2个 == 素数

第2个 + 第3个 == 素数

第n - 2个 + 第n-1个 == 素数

第n-1个 + 第n个 == 素数

第一个 + 第n个 也是素数!!  形成闭环

DFS + 回溯

#include <iostream>
#include <vector>
using namespace std;


int isPrime[101];


vector<int> prime;
bool mark[101]; // vector中,数字是否使用


int circle[21]; // 1 ~ n,用来存储结果

int n;

void print(int a[]) {
	for (int i = 1; i <= n - 1; i++) {
		cout << a[i] << " ";
	}
	cout << a[n];
	cout << endl;

}
void print(vector<int> a) {
	for (int i = 0; i <= a.size() - 1; i++) {
		cout << a[i] << " ";
	}
	cout << endl;

}

void initialize(int maxIndex) {

	isPrime[0] = isPrime[1] = 0;

	for (int i = 2; i <= maxIndex; i++) {
		isPrime[i] = 1;
	}


	for (int i = 2; i <= maxIndex; i++) {
		if (isPrime[i] == 1) {
			for (int j = 2 * i; j <= maxIndex; j += i) {
				isPrime[j] = 0;
			}
			prime.push_back(i);
		}
	}


}


void checkAndPrint() {
	if (isPrime[circle[1] + circle[n]]) {
		print(circle);
	}
}



void dfs(int step) {
	if (step == n + 1) {
		checkAndPrint();//符合就输出
		return ;
	}

	for (int i = 2; i <= n; i++) {
		if (mark[i] == 0 && isPrime[circle[step - 1] + i] == 1 ) {
			circle[step] = i;
			mark[i] = 1;
			dfs(step + 1);
			mark[i] = 0;


		}

	}

}



int main() {

	initialize(100);
	int k=0;
	bool firstCase = true;

	while(cin >> n) {
		circle[1] = 1;
		mark[1] = 1;

		for (int i = 1; i <= n; i++) {
			mark[i] = 0;
		}
		/*
		if (firstCase) {
			firstCase = false;
		} else {
			printf("\n");
		}
		*/
		printf("Case %d:\n",++k);
		dfs(2); // 从2 - n
		cout << endl;
	}

}
#include <iostream>

using namespace std;

bool isPrime[101];


int n;  // 1 ~ n
bool mark[101]; // 1 ~ 100 是否使用过
int res[101]; // 最终的素数环



void initialize(int maxIndex) {
	isPrime[0] = isPrime[1] = 0;

	// 这个for循环 竟然忘了 直接写了下面那个for! 唉!
	for (int i = 2; i <= maxIndex; i++) {
		isPrime[i] = 1;
	}

	for (int i = 2; i <= maxIndex; i++) {
		if (isPrime[i] == 1) {
			for (int j = 2 * i ; j <= maxIndex ; j += i) {
				isPrime[j] = 0;
			}
		}
	}
}

void dfs(int index) {

	if (index > n) {
		for (int i = 1; i <= n; i ++) {
			cout << res[i] << " ";
		}
		cout << endl;
	} else if (index == n) {
		for (int i = 2; i <= n; i++) {
			if (mark[i] == 0 && isPrime[res[index-1] + i] == 1 && isPrime[res[1] + i] == 1) {
				mark[i] = 1;
				res[index] = i;
				dfs(index + 1);
				mark[i] = 0;
			}
		}
	} else {
		// 前 2 ~ n -1 位   满足 与前一位成 素数 就可以了
		for (int i = 2; i <= n; i++) {
			if (mark[i] == 0 && isPrime[res[index-1] + i] == 1) {
				mark[i] = 1;
				res[index] = i;
				dfs(index + 1);
				mark[i] = 0;
			}
		}
	}
}

int main() {
	initialize(100);


	while (cin >> n) {

		for (int i = 1; i <= n; i ++) {
			mark[i] = 0;
		}

		res[1] = 1;
		mark[1] = 1;
		dfs(2);

	}
}
发布了86 篇原创文章 · 获赞 0 · 访问量 3662

猜你喜欢

转载自blog.csdn.net/bijingrui/article/details/104366107
今日推荐