UVa 524 Prime Ring Problem【回溯】
2024-10-21 20:35:18
题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列
照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL;
int a[],vis[],isp[];
int n; void is_prime(){
isp[]=isp[]=;
for(int i=;i<=;i++){
if(isp[i]==){
for(int j=i*;j<=;j+=i)
isp[j]=;
}
}
} void dfs(int cur){
int i;
if(cur==n&&!isp[a[]+a[n-]]){
for( i=;i<n-;i++) printf("%d ",a[i]);
printf("%d\n",a[i]);
} else for(i=;i<=n;i++)
if(!vis[i]&&!isp[i+a[cur-]]){
a[cur]=i;
vis[i]=;
dfs(cur+);
vis[i]=;
}
} int main(){
int t=;
is_prime();
while(cin>>n){
memset(vis,,sizeof(vis));
a[] = ;
vis[] = ;
if(t) printf("\n");
printf("Case %d:\n", ++t);
dfs();
}
return ;
}
自己写的时候,直接是dfs(0),什么都输不出来,可是为什么是这样呢= = 学习回溯的第一题目 = = 再好好理解- -
最新文章
- BZOJ4596: [Shoi2016]黑暗前的幻想乡
- centos7安装python3
- Objective-c防止数组越界而崩溃(全局效果)
- 使用FIR.im发布自己的移动端APP
- XStream使用总结
- Android - 广播接收者 - BroadcastReceiver
- Asp.net MVC中 Controller 与 View之间的数据传递
- 关于iis7短日期后面带有星期的解决办法
- 关于MVC4中EFCoderFirst 数据迁移的三句经典指令
- 看人家如何拿到腾讯阿里的offer
- 利用hadoop自带程序运行wordcount
- 最新的Android SDK安装攻略(动作要快,来晚就失效了)
- 简单总结下opacity、transparent以及rgba
- ( 转)Ubuntu下创建、重命名、删除文件及文件夹,强制清空回收站方法
- Spark2.1.0——内置RPC框架详解
- LOJ 10138 -「一本通 4.5 例 1」树的统计
- 循序渐进学.Net Core Web Api开发系列【0】:序言与目录
- easyui------修改validatebox过滤规则
- Vultr VPS建站攻略 – 一键安装宝塔面板架设LNMP/LAMP Web环境
- [T-ARA][Lovey-Dovey]
热门文章
- cadence16.6 中orcad导出网表时ERROR (ORCAP-5004)
- Python标准库之os模块
- [sql server、oracle] 分组取最大值最小值常用sql
- Codeforces Round #327 (Div. 1) B. Chip &#39;n Dale Rescue Rangers 二分
- firefox同步ajax请求报错的问题 A parameter or an operation is not supported by the underlying object
- Unity3d 一些 常见路径
- mysql数据库主外键级联删除脚本RESTRICT -->; CASCADE
- POJ 2251 Dungeon Master(广搜,三维,简单)
- ZOJ 3757 Alice and Bob and Cue Sports(模拟)
- hdu 4701 Game 博弈论