Prime Ring Problem-dfs
2018-01-01 14:20
183 查看
HDU-1016
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 56697 Accepted Submission(s): 24925
[align=left]Problem Description[/align]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.
[align=left]Input[/align]n (0 < n < 20).
[align=left]Output[/align]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.
[align=left]Sample Input[/align]68 [align=left]Sample Output[/align]Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2
#include<stdio.h>
#include<math.h>
#include<string.h>
int a[30],n,prime[50],visit[22];
int judge(int c)
{
if(c==n-1)//检查最后一个数
{
if((prime[a[n-1]+1]==1)&&prime[a[c]+a[c-1]])
return 1;
return 0;
}
if(prime[a[c]+a[c-1]])
return 1;
return 0;
}
void dfs(int c)
{
if(c==n)
{
for(int i=0;i<n-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[n-1]);
return ;
}
for(int i=2;i<=n;i++)
{
a[c]=i;
if(judge(c)&&visit[i]==0)
{
visit[i]=1;
dfs(c+1);
visit[i]=0;
}
a[c]=0;//
}
}
int main()
{
memset(prime,0,sizeof(prime));//素数打表
prime[2]=1;prime[3]=1;prime[5]=1;prime[7]=1;prime[11]=1;
prime[13]=1;prime[17]=1;prime[19]=1;prime[23]=1;prime[29]=1;prime[31]=1;prime[37]=1;
int t=1;
while(scanf("%d",&n)!=EOF)
{
memset(visit,0,sizeof(visit));
a[0]=1;
printf("Case %d:\n",t++);
dfs(1);
printf("\n");
}
}
1128 - 组合数
时间限制:3秒 内存限制:128兆193 次提交 113 次通过
题目描述找出从自然数1、2、... 、n(0<n<10)中任取r(0<r<=n)个数的所有组合。输入输入n、r。输出按特定顺序输出所有组合。
特定顺序:每一个组合中的值从大到小排列,组合之间按逆字典序排列。样例输入
5 3样例输出
543 542 541 532 531 521 432 431 421 321
#include<iostream>
using namespace std;
int n,r,a[10];
void dfs(int c,int x)
{
int i;
if(x==r+1)
{
for(i=1;i<=r;i++)
cout<<a[i];
cout<<endl;
}
for(i=c;i>=1;i--)
{
a[x]=i;
dfs(i-1,x+1);
}
}
int main()
{
while(scanf("%d%d",&n,&r)!=EOF)
dfs(n,1);
}
1154 - 最少步数
时间限制:3秒 内存限制:128兆179 次提交 79 次通过
题目描述这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1
1,0,0,1,0,0,1,0,1
1,0,0,1,1,0,0,0,1
1,0,1,0,1,1,0,1,1
1,0,0,0,0,1,0,0,1
1,1,0,1,
4000
0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,0,0,0,1
1,1,1,1,1,1,1,1,10表示道路,1表示墙。现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)输入第一行输入一个整数n(0<n<=100),表示有n组测试数据;
随后n行,每行有四个整数a,b,c,d(0<=a,b,c,d<=8)分别表示起点的行、列,终点的行、列。输出输出最少走几步。样例输入
2 3 1 5 7 3 1 6 7样例输出
12 11
#include<iostream> using namespace std; int min(int a,int b) {return a<b? a:b;} int x1,y1,x2,y2,s,ans; int dx[]={-1,1,0,0}; int dy[]={0,0,-1,1}; int m[9][9]={ 1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 1,0,0,0,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1}; void dfs(int x,int y,int c) { if(x==x2&&y==y2) { ans=min(ans,c); return ; } for(int i=0;i<4;i++) { int xx=x+dx[i],yy=y+dy[i]; if(m[xx][yy]==0) { m[xx][yy]=1; dfs(xx,yy,c+1); m[xx][yy]=0; } } } int main() { int t; cin>>t; while(t--) { ans=99999; cin>>x1>>y1>>x2>>y2; dfs(x1,y1,0); cout<<ans<<endl; } }
1115 - 擅长排列的小明
时间限制:1秒 内存限制:128兆94 次提交 58 次通过
题目描述小明十分聪明,而且十分擅长排列计算。比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长。现在需要你写一个程序来验证擅长排列的小明到底对不对。输入第一行输入整数N(1<N<10)表示多少组测试数据,
每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)输出在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。如样例样例输入
2 3 1 4 2样例输出
1 2 3 12 13 14 21 23 24 31 32 34 41 42 43
#include<iostream>
#include<cstring>
using namespace std;
int n,m,a[10];
int dge(int c)
{
for(int i=0;i<c;i++)
if(a[i]==a[c])
return 0;
return 1;
}
void dfs(int c)
{
if(c==m)
{
for(int i=0;i<m;i++)
cout<<a[i];
cout<<endl;
return ;
}
for(int i=1;i<=n;i++)
{
a[c]=i;
if(dge(c))
dfs(c+1);
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
memset(a,0,sizeof(a));
cin>>n>>m;
dfs(0);
}
}
相关文章推荐
- HDU1016 Prime Ring Problem(DFS回溯)
- HDU 1016 Prime Ring Problem DFS + 记忆化搜索
- hdoj 1016 Prime Ring Problem 【DFS】
- zoj1457 Prime Ring Problem DFS
- UVA524 UVALive5270 HDU1016 ZOJ1457 Prime Ring Problem【DFS】
- 杭电1016 Prime Ring Problem(DFS)
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
- POJ - 1016 - Prime Ring Problem(dfs)
- HDU 1016 Prime Ring Problem DFS
- HDU 1016 Prime Ring Problem(DFS)
- [ACM] hdu 1016 Prime Ring Problem (DFS)
- HDU 1016 - Prime Ring Problem (DFS)
- HDU 1016 Prime Ring Problem(搜索入门题 DFS,多种方式)
- HDOJ 1016 Prime Ring Problem (DFS)
- Prime Ring Problem (dfs)
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
- HDU 1016 Prime Ring Problem(dfs)
- HDU-1016 Prime Ring Problem (DFS)
- HDU 1016 Prime Ring Problem(DFS)
- 搜索专题(DFS)HDU 1016-Prime Ring Problem