站点图标 陌路寒暄

Prime Ring Problem

Prime Ring Problem

时间: 1ms        内存:128M

描述:

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.

输入:

n (0 < n < 20).

输出:

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.

The followings are programmed codes. Your task is to complete function dfs(). When you submit your program, you just submit only function dfs(). The rest of program will be added to your program automatically.

//The following codes will become the front of your program automatically.
#include<stdio.h>
#include<string.h>
int n,ans[21],isprime[]= {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0},vis[21];

//The code you must submit
void dfs(int step)
{
    ...
}

//The following codes will become the rear of your program automatically.
int main()
{
    int count;
    count=0;
    while(scanf("%d",&n)!=EOF)
    {
        count++;
        ans[1]=1;
        memset(vis,0,sizeof(vis));
        printf("Case %d:\n",count);
        dfs(2);
        printf("\n");
    }
    return 0;
}

示例输入:

6
8

示例输出:

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

提示:

参考答案(内存最优[752]):

#include<stdio.h>
#include<string.h>
int n,ans[21],isprime[]= {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0},vis[21];
//The code you must submit
void dfs(int step)
{
	int i;
	if(step>n)
	{
		for(i=1;i<n;i++)
			printf("%d ",ans[i]);
		printf("%d\n",ans[n]);
		return ;
	}
	for(i=2;i<=n;i++)
	{
		if(vis[i])
			continue;
		if(isprime[ans[step-1]+i]==0)
			continue;
		if(step==n&&isprime[i+1]==0)
			continue;
		ans[step]=i;
		vis[i]=1;
		dfs(step+1);
		vis[i]=0;
	}

}

//The followingcodes will become the rear of your program automatically. 
int main()
{
    int count;
    count=0;
    while(scanf("%d",&n)!=EOF)
    {
        count++;
        ans[1]=1;
        memset(vis,0,sizeof(vis));
        printf("Case %d:\n",count);
		vis[1]=1;
        dfs(2);
        printf("\n");
    }
    return 0;
}

参考答案(时间最优[0]):


#include<stdio.h>
#include<string.h>
int n,ans[21],isprime[]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0},vis[21];
void dfs(int step)
{
    int i;
    if(step==n+1)
    {
        if(isprime[ans[n]+1])
            for(i=1;i<=n;i++)
                printf("%d%c",ans[i],i==n?'\n':' ');
    }
    else
        for(i=2;i<=n;i++)
            if(!vis[i]&&isprime[i+ans[step-1]])
            {
                ans[step]=i;
                vis[i]=1;
                dfs(step+1);
                vis[i]=0;
            }
}
int main()
{
    int count;
    count=0;
    while(scanf("%d",&n)!=EOF)
    {
        count++;
        ans[1]=1;
        memset(vis,0,sizeof(vis));
        printf("Case %d:\n",count);
        dfs(2);
        printf("\n");
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版