1. 程式人生 > >杭電1016(dfs)增所廣收

杭電1016(dfs)增所廣收

</pre><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url("panel-content.png") repeat-y;">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.<img src="http://acm.hdu.edu.cn/data/images/1016-1.gif" style="border: none; max-width: 100%;" alt="" /></div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url("panel-bottom.png") 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url("panel-title.png") 0% 100% no-repeat transparent;">Input</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url("panel-content.png") repeat-y;">n (0 < n < 20).</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url("panel-bottom.png") 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url("panel-title.png") 0% 100% no-repeat transparent;">Output</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url("panel-content.png") repeat-y;">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.</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url("panel-bottom.png") 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url("panel-title.png") 0% 100% no-repeat transparent;">Sample Input</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url("panel-content.png") repeat-y;"><pre style="word-wrap: break-word; white-space: pre-wrap; margin-top: 0px; margin-bottom: 0px;"><div style="font-family: 'Courier New', Courier, monospace;">6
8</div>

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 一個環,相鄰兩個數之和是素數,,並且把所以可能都按順序輸出來。 
#include <iostream>
#include <math.h>
using namespace std;
#define max 21
int vis[max],hk[max];
int n,k=1;
bool ls(int a,int b)
{
	int c=a+b;
	for(int i=2;i*i<=c;i++)
	{
		if(c%i==0)
			return false;
	}
	return true;
}
void DFS(int a)
{
	if(n==a&&ls(1,hk[n-1]))
	{
		cout<<hk[0];
		int i;
		for(i=1;i<n;i++)
		{
			cout<<' '<<hk[i];
		}
		cout<<endl;
	}
	else
	{
		int i;
		for(i=2;i<=n;i++)
		{
			if(vis[i]==true||!ls(hk[a-1],i))
			{
				continue;
			}
			hk[a]=i;
			vis[i]=true;
			DFS(a+1);
			vis[i]=false;
		}
	}
}
int main()
{
	hk[0]=1;
	memset(vis,false,sizeof(vis));
	while(cin>>n)
	{
		cout<<"Case "<<k++<<':'<<endl;
		DFS(1);
		cout<<endl;
	}
	return 0;
}