hdu1016 Prime Ring Problem(dfs)
来源:互联网 发布:二线演员年收入知乎 编辑:程序博客网 时间:2024/05/29 15:43
这个题目虽然是dfs的初级题目,但是我对dfs几乎是一窍不通,所以写这个题也挺费劲的,不过写了这个之后再写别的题目就很容易了,这题是要找素数环,一个非常基础的深搜
#include <iostream>#include <string.h>using namespace std;int prime[41]={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};int n;bool vis[25];int a[25];void dfs(int pos)//这里面传的是位置{ //cout<<pos<<endl; if(pos==n+1&&prime[a[n]+a[1]])//这里一定是n+1,因为在n的时候,第n位还没有处理 { for(int i=1;i<pos-1;i++) { cout<<a[i]<<" "; } cout<<a[pos-1]<<endl; //cout<<endl; return; } for(int i=2;i<=n;i++) { if(!vis[i]&&prime[i+a[pos-1]]) { a[pos]=i; vis[i]=true; dfs(pos+1); vis[i]=false; } }}int main(){ int cases=1; while(cin>>n) { cout<<"Case "<<cases++<<":"<<endl; memset(vis,0,sizeof(vis)); a[1]=1; vis[1]=true; dfs(2); cout<<endl; } return 0;}
0 0
- HDU1016-Prime Ring Problem(DFS)
- hdu1016(Prime Ring Problem)- dfs+枚举
- hdu1016 Prime Ring Problem(dfs)
- hdu1016 Prime Ring Problem(dfs)
- hdu1016 Prime Ring Problem(回溯dfs)
- hdu1016 Prime Ring Problem(dfs)
- HDU1016 Prime Ring Problem (DFS)
- HDU1016 Prime Ring Problem(dfs)
- HDU1016:Prime Ring Problem(dfs)
- HDU1016:Prime Ring Problem(DFS)
- hdu1016 Prime Ring Problem(DFS)
- Hdu1016 Prime Ring Problem(DFS)
- HDU1016:Prime Ring Problem(DFS)
- HDU1016:Prime Ring Problem(DFS)
- hdu1016(Prime Ring Problem)
- hdu1016 Prime Ring Problem()
- HDU1016——Prime Ring Problem(dfs,输入格式)
- HDU1016 Prime Ring Problem 解题报告--dfs
- 误导口负担而战 经验
- 学习1
- 【codeforces 722C】【逆向思维 离线+并查集】C. Destroying Array 【给你n个数,每次摧毁一个,求每摧毁一个的最大连续和(被摧毁的点的两边不连续)】
- 一个 Linux 上分析死锁的简单方法
- 万事不要太过强求
- hdu1016 Prime Ring Problem(dfs)
- 《机器学习》学习笔记一
- 初学Java Socket编程实现简单的问候服务
- 阿里云服务器搭建
- 722C - Destroying Array 线段树
- Swift学习记录 -- 7.数组的基本操作
- Why OpenCV is slower than Matlab in matrix related operation,如:特征值分解,奇异值分解
- USB枚举过程
- ubuntu16.04安装最新版本的wine