Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 18 Accepted Submission(s) : 7
Note: the number of first circle should always be 1.
#include<stdio.h>
#include<string.h>
int n,t,z[],mark[];
//int m[10010][21];
bool isprime(int x){
if(x==||x==)return false;
for(int i=;i<x;i++){
if(x%i==)return false;
}
return true;
}
void dfs(int flot){
if(flot>=n){
for(int i=;i<n;i++){
//m[t][i]=z[i];
if(i)printf(" ");
printf("%d",z[i]);
}puts("");
t++;return;
}
for(int i=;i<=n;i++){
if(isprime(z[flot-]+i)&&!mark[i]){if(flot==n-){
if(!isprime(+i))break;
}//判断首尾;
mark[i]=;
z[flot]=i;
dfs(flot+);
mark[i]=;
}
//else dfs(top-1,flot);
}
return ;
}
int main(){
int k=;
while(~scanf("%d",&n)){
k++;
printf("Case %d:\n",k);
memset(mark,,sizeof(mark));
z[]=;
dfs();
puts("");
}
return ;
}
素数环
- 描述
-
有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环。
为了简便起见,我们规定每个素数环都从1开始。例如,下图就是6的一个素数环。
- 输入
- 有多组测试数据,每组输入一个n(0<n<20),n=0表示输入结束。
- 输出
- 每组第一行输出对应的Case序号,从1开始。
如果存在满足题意叙述的素数环,从小到大输出。
否则输出No Answer。 - 样例输入
-
6
8
3
0 - 样例输出
-
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
Case 3:
No Answer 题解:由于当n是奇数时肯定组不成素数,所以当n是奇数时,如果不是1就是no anwser;
代码:#include<stdio.h>
#include<string.h>
const int MAXN=;
int n,flot;
int vis[MAXN],ans[MAXN];
bool isprime(int x){
if(x==||x==)return false;
for(int i=;i<x;i++){
if(x%i==)return false;
}
return true;
}
void dfs(int num){
if(num==n){
if(isprime(ans[num-]+ans[])){
flot=;
for(int i=;i<num;i++){
if(i)printf(" ");
printf("%d",ans[i]);
}
puts("");
}
return;
}
for(int i=;i<=n;i++){
if(vis[i]||!isprime(i+ans[num-]))continue;
vis[i]=;ans[num]=i;
dfs(num+);
vis[i]=;
}
}
int main(){
int t=;
while(~scanf("%d",&n),n){
memset(vis,,sizeof(vis));
printf("Case %d:\n",++t);
if(n&){
if(n==)puts("");
else puts("No Answer");
}
else{
flot=;
ans[]=;
dfs();
if(!flot)puts("No Answer");
}
}
return ;
}
Oil Deposits
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 62 Accepted Submission(s) : 40
#include<stdio.h>
int m,n;
char map[][];
void dfs(int x,int y){
if(map[y][x]=='*'||x<||x>=n||y<||y>=m)return ;
map[y][x]='*';
dfs(x+,y);
dfs(x,y+);
dfs(x-,y);
dfs(x,y-);
dfs(x+,y+);
dfs(x-,y-);
dfs(x-,y+);
dfs(x+,y-);
}
int main(){int tot;
while(~scanf("%d%d",&m,&n),m||n){tot=;
for(int y=;y<m;y++)scanf("%s",map[y]);
for(int y=;y<m;y++){
for(int x=;x<n;x++){
if(map[y][x]=='@'){
dfs(x,y);tot++;
}
}
}
printf("%d\n",tot);
}
return ;}
Red and Black
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 71 Accepted Submission(s) : 65
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black的更多相关文章
- HDU 1016 Prime Ring Problem(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA - 524 Prime Ring Problem(素数环)(回溯法)
题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") ...
- HDU 1016 Prime Ring Problem (素数筛+DFS)
题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... #include <cstd ...
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 题目1459:Prime ring problem(素数环问题——递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
随机推荐
- UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>
D - 基爷的中位数 Time Limit: 5000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- Gas Station 解答
Problem There are N gas stations along a circular route, where the amount of gas at station i is gas ...
- 配置VirtualBox Linux系统与Windows共享文件
手动方式配置VirtualBox Linux系统与Windows共享文件 首先,要安装VirtualBox自带的扩展工具,这个ISO可以在虚拟主机安装目录下找到. 将这个文件Copy到你的Linux主 ...
- REDHAT、CenterOS使用安装Linux系统时的光盘镜像来安装软件
使用安装Linux系统时的光盘镜像来安装软件 (1)以虚拟机上,安装mysql为例: 查看mysql是否安装 rpm -qa|grep -i mysql 显示下面,证明mysql已安装客户端,下 ...
- java多线程 并发 编程
转自:http://www.cnblogs.com/luxiaoxun/p/3870265.html 一.多线程的优缺点 多线程的优点: 1)资源利用率更好 2)程序设计在某些情况下更简单 3)程序响 ...
- android scrollview 简单的使用
以前写的Scrollview ,通常都是与Listview结合使用,不过因复杂可能新手不太懂,网上有许多文章,这里就不贴那个了DEMO了. 写了个简单的供大家参考:这样比较好理解(需要复杂的可以Q我 ...
- HDU 2030 统计汉字
BestCoder官方群:385386683 欢迎加入~ 寻人启事:2014级新生看过来! 汉字统计 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- JDBC插入百万数据,不到5秒!
java自带的批量操作,就可以很好的支持大量数据的处理.相比c#,简单很多.c#要使用oracle提供的ODP.NET,效率才很高,但是代码却很复杂.总之,在这方面,c#没得比.当然,这里的表是没加索 ...
- 初探JS-html5移动端发送指定内容短信到指定号码
原理:利用a标签跳转指定网址: sms://[号码]?body=[内容] //安卓 sms://[号码]&body=[内容] //IOS 首先简单的做两个input,一个用于输入内容,一个用于 ...
- A child container failed during start 解决方案
症状:A child container failed during start 适用问题描述:tomcat挂空可以正常运行,载入项目时挂掉. 相关操作:之前为了省事,由于两个servlet功能类似所 ...