1.数的划分

点击查看搜索
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,a[100];
void dfs(int x,int y,int z)//shu zong wei
{
if(y>n)return;
if(y==n)
{
for(int i=1;i<z;i++)//不取等
{
printf("%d ",a[i]);
}
printf("\n");
return;
}
for(int i=x;i<n;i++)
{
a[z]=i;
dfs(i,y+i,z+1);
}
}
int main()
{
scanf("%d",&n);
dfs(1,0,1);
return 0;
}
点击查看递推
/*把n看成是n个小球,k看成是k个盒子

  那么题目就变成了把n小球放到k个盒子,且每个盒子都至少有1个小球的问题。

  那么把n个小球放到k个盒子里的情况总数 = 1.至少有1个盒子放1个小球的情况总数 + 2.每个盒子都有多于1个小球的情况总数

  那么这里 1 相当于 把n-1个小球放到k-1个盒子里的情况总数

  2 相当于 把n-k个小球放到k个盒子里的情况总数

  到了这里就可以发现是递推咯。

  公式为 f(n,k) = f(n-1,k-1) + f(n-k,k)
*/
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, k, f[205][10];
int main() {
scanf("%d%d", &n, &k);//不太一样,划分成k个数
for (int i = 1; i <= n; i++) {
f[i][1] = 1;
}
for (int i = 2; i <= k; i++) {
f[i][i] = 1;
for (int j = i + 1; j <= n; j++) {
f[j][i] += f[j - 1][i - 1] + f[j - i][i];
}
}
printf("%d", f[n][k]);
return 0;
}

2.组合数的输出

点击查看代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,a[100];
bool b[100];
void print()
{
for(int i=1;i<=m;i++)printf("%d ",a[i]);
printf("\n");
return;
}
int dfs(int x,int y)
{
if(y>m) print();
else
{
for(int i=x;i<=n;i++)
{
a[y]=i;
dfs(i+1,y+1);
}
}
}
int main()
{
scanf("%d%d",&n,&m);
dfs(1,1);
return 0;
}

3.拔河比赛

详见Ybtoj

当然重点在于搜索都配有板子了

点击查看1
int search(int k)
{
for(int i=1;i<=n;i++)
{
if()//合法
{
//保存
if()//终点
{
ans++;
print();
}
else search(++k);
}
//恢复(回溯)
}
}
点击查看2
int search(int k)
{
if()//
{
ans++;
print();
}
else for(int i=1;i<=n;i++)
{
if()
{
//
search(++k);
//
}
}
}

注意场宽 && STL大法好

题目描述

第一行:n

第二行:n个字符(有重复)

输出:全排列+方案数

思路:把单一的 vis[i] 换为累加器数组

STL大法好
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string.h>
#include<algorithm>
int n;
char s[30];
int main()
{
long long ans = 0;
scanf("%d",&n);
scanf("%s",s+1);
std::sort(s+1,s+n+1);
do{
for(int i=1;i<=n;i++) printf("%c",s[i]);
printf("\n");
++ans;
}while(std::next_permutation(s+1,s+n+1));
printf("%lld\n", ans);
return 0;
}
正解
#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string.h>
using namespace std;
int n , ans = 0;
char s[100];
bool same( int t, int k) {
for(int i = t; i < k; ++i) {
if(s[k] == s[i]){
return 0;
}
}
return 1;
}
void print()
{
for(int i = 0; i < n; i++) {
printf("%c",s[i]);
}
printf("\n");
}
void dfs(int t) {
if(t == n-1) {
print();
ans++;
}
for(int i = t; i < n; ++i){
if(same(t, i)) {
swap(s[i], s[t]);
dfs( t + 1 );
swap(s[i], s[t]);
}
}
}
int main() {
scanf("%d",&n);
cin >> s;
dfs( 0);
printf("%d",ans);
return 0;
}

//数组尽量开大一点

dfs+search的更多相关文章

  1. 1923. Scary Politics (timus) (dfs) search

    http://acm.timus.ru/problem.aspx?space=1&num=1923 -- timus This is s problem about thd dfs and s ...

  2. Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

    1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...

  3. [LeetCode#212]Word Search II

    Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...

  4. Leetcode: Android Unlock Patterns

    Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...

  5. PE刷题记录

    PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...

  6. Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  7. Trie for string LeetCode

    Trie build and search class TrieNode { public: TrieNode * next[]; bool is_word; TrieNode(bool b = fa ...

  8. 582. Kill Process

    Problem statement: Given n processes, each process has a unique PID (process id) and its PPID (paren ...

  9. Leetcode: Closest Leaf in a Binary Tree

    Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...

随机推荐

  1. Word2010初识

    原文链接:https://www.toutiao.com/i6487370439910752782/ 认识Word Microsoft Office Word是微软公司的一个文字处理器应用软件. 启动 ...

  2. Word2010制作倒福字

    原文: https://www.toutiao.com/i6489772955797553677/ 选择"插入"选项卡,"插图"功能组,"形状&quo ...

  3. antd的table组件设置Column的width列宽度不生效问题

    超长连续字段(长数字和长单词) 破坏表格布局的问题(即使你指定了列的宽度也会被挤开),之前组件内默认加过 word-break: break-word; 去纠正此类布局,又会引起其他的问题. 所以最好 ...

  4. HIVE执行引擎TEZ学习以及实际使用

    概述 最近公司在使用Tez,今天写一篇关于Tez的学习和使用随笔.Tez是Apache最新的支持DAG作业的开源计算框架,它可以将多个有依赖的作业转换为一个作业从而大幅提升DAG作业的性能.Tez并不 ...

  5. Keil MDK STM32系列(五) 使用STM32CubeMX创建项目基础结构

    Keil MDK STM32系列 Keil MDK STM32系列(一) 基于标准外设库SPL的STM32F103开发 Keil MDK STM32系列(二) 基于标准外设库SPL的STM32F401 ...

  6. Java不限制从键盘输入一个数组

    Java不限制从键盘输入一个数组 在写算法的时候,需要从键盘输入一个数组,一直不会,最近看了几篇博客学会了,下面用二分查找举例: package com.基础; import java.util.Sc ...

  7. 《剑指offer》面试题65. 不用加减乘除做加法

    问题描述 写一个函数,求两个整数之和,要求在函数体内不得使用 "+"."-"."*"."/" 四则运算符号. 示例: 输 ...

  8. leetcode 46. 全排列 及 47. 全排列 II

    46. 全排列 问题描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3 ...

  9. Android 12(S) 图形显示系统 - 基本概念(一)

    1 前言 Android图形系统是系统框架中一个非常重要的子系统,与其它子系统一样,Android 框架提供了各种用于 2D 和 3D 图形渲染的 API供开发者使用来创建绚丽多彩的应用APP.图形渲 ...

  10. golang中结构体指针的应用

    package main import ( "fmt" ) type School struct { brand string city string } type Class s ...