深搜最基础题---全排列And组合数
这个是理解标记和取消标记,用一个vis数组来标记
全排列代码:
#include <stdio.h> int a[];
int vis[];
int n;
void dfs(int step)//step是当前已经进去排列的个数
{
if (step == n)//如果找到n个之后,打印一次
{
for (int i = ; i < n; i++)
printf("%d ", a[i]);
puts("");
return ;
}
for (int i = ; i <= n; i++)
{
if (vis[i] == )//排列中没有此数字,就将它加入到排列中
{
vis[i] = ;
a[step] = i;
dfs(step + );
vis[i] = ;
}
}
}
int main()
{
while (scanf("%d", &n) == )
{
dfs();
}
return ;
}
组合数代码(NYOJ-32):
#include <stdio.h>
#include <string.h> int n, r;
int a[];
int vis[];
void dfs(int step)
{
if (step == r + )
{
for (int i = ; i <= r; i++)
printf("%d", a[i]);
puts("");
return ;
}
for (int i = n; i > ; i--)
{
if (vis[i] == && i < a[step - ])//满足逆序
{
vis[i] = ;
a[step] = i;
dfs(step + );
vis[i] = ;
}
}
}
int main()
{
a[] = ;
while (scanf("%d %d", &n, &r) == )
{
memset(vis, , sizeof(vis)); dfs();
} return ;
}
深搜最基础题---全排列And组合数的更多相关文章
- HDU 4597 Play Game(记忆化搜索,深搜)
题目 //传说中的记忆化搜索,好吧,就是用深搜//多做题吧,,这个解法是搜来的,蛮好理解的 //题目大意:给出两堆牌,只能从最上和最下取,然后两个人轮流取,都按照自己最优的策略,//问说第一个人对多的 ...
- PTA 7-6 列出连通集(深搜+广搜)
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...
- NYOJ 10 skiing (深搜和动归)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪. 由于滑雪的确非常刺激.但是为了获得速度.滑的区域必须向下倾斜.并且 ...
- [codevs1049]棋盘染色<迭代深搜>
题目链接:http://codevs.cn/problem/1049/ 昨天的测试题里没有打出那可爱的迭代深搜,所以今天就来练一练. 这道题其实我看着有点懵,拿着题我就这状态↓ 然后我偷偷瞄了一眼hz ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 5648 DZY Loves Math 组合数+深搜(子集法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给定n,m(1<= n,m <= 15,000),求Σgcd(i|j,i&am ...
- hdu1015+hdu1016 都是十分钟以内的深搜题
hdu1015:给定一串可用序列值,每个字符映射到一个1-26之间的整数,选择五个有序数使得满足 a-b2+c3-d4+e5=target. #include<iostream> #inc ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- 深搜基础题目 杭电 HDU 1241
HDU 1241 是深搜算法的入门题目,递归实现. 原题目传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1241 代码仅供参考,c++实现: #incl ...
随机推荐
- 理解javascript之 对象
大纲: 1.介绍attribute property的异同,翻译自http://javascript.info/tutorial/attributes-and-custom-properties#pr ...
- Objective-C中的协议(Protocol)和类别(Category)
1.什么是协议? 2.协议与类别的声明和使用 1.什么是协议? 在Objective-C中,不支持多继承,即不允许一个类有多个父类,但是OC提供了类似的实现方法,也就是协议.协议有点类似于Java里的 ...
- python密码处理(可用于生产模式)
import os from hashlib import sha256 from hmac import HMAC def encrypt_password(password, salt=None) ...
- python之sys模块
38.python的sys模块: 用于提供对Python解释器相关的操作: 1 2 3 4 5 6 7 8 9 sys.argv 命令行参数List,第一个元素是程序本身路径 sy ...
- Day3 set集合、函数和装饰器
set特性:无序,不重复,可嵌套 创建setset_example = set("123", "213", "234", "432 ...
- CodeForces 546C(队列)
CodeForces 546C Soldier and Cards Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I ...
- js 冒泡事件的处理
onMouseOver 和 onMouseOut事件冒泡 当事件在某一DOM元素被触发时,例如用户在某个节点上点击鼠标,事件将跟随着该节点继承的各个父节点冒泡穿过整个DOM的节点层次,直到它遇到依附有 ...
- Dapper快速学习
Dapper快速学习 我们都知道ORM全称叫做Object Relationship Mapper,也就是可以用object来map我们的db,而且市面上的orm框架有很多,其中有一个框架 叫做dap ...
- UI基础 - UITabBarController
self.window = [[UIWindow alloc] init]; self.window.frame = [UIScreen mainScreen].bounds; oneViewCont ...
- json-lib 使用教程
//关于java map与JSONObject类互相转换 Map<String,Object> map=new HashMap<String,Object>(); map.pu ...