模板

void dfs()//参数用来表示状态
{
if(到达终点状态)
{
...//根据题意添加
return;
}
if(越界或者是不合法状态)
return;
if(特殊状态)//剪枝
return ;
for(扩展方式)
{
if(扩展方式所达到状态合法)
{
修改操作;//根据题意来添加
标记;
dfs();
(还原标记);
//是否还原标记根据题意
//如果加上(还原标记)就是 回溯法
}
}
}

46. 全排列

class Solution {
public:
vector<vector<int>> res;
vector<int> vis;
vector<vector<int>> permute(vector<int>& nums) {
vector<int> path;
vis.resize(nums.size(), 0);
dfs(nums, path, 0);
return res;
}
void dfs(vector<int> & nums, vector<int>& path, int len){//参数表示状态
if(len == nums.size()){//递归出口
res.push_back(path);return;
}
for(int i = 0;i < nums.size(); i++){//扩展方式
if(vis[i] == 0){//扩展方式合法
path.push_back(nums[i]);
vis[i] = 1;
dfs(nums,path,len + 1);
vis[i] = 0;
path.pop_back();
}
}
}
};

47. 全排列 II 排序去重

class Solution {
public:
vector<vector<int>> res;
vector<int> vis;
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(),nums.end());
vis.resize(nums.size(),0);
vector<int> path;
dfs(nums,path, 0);
return res;
}
void dfs(vector<int>& nums, vector<int>& path, int depth){
if(depth == nums.size()){//递归出口
res.push_back(path);
return;
}
for(int i = 0; i < nums.size(); i++){
//在同一深度只会遍历第一个相同的数字,不同深度时vis[i-1] = 1
if(i && nums[i] == nums[i-1] && vis[i-1] == 0) continue;
if(vis[i] == 0){
vis[i] = 1;
path.push_back(nums[i]);
dfs(nums,path,depth + 1);
vis[i] = 0;
path.pop_back();
}
}
}
};

491. 递增子序列

class Solution {
public:
vector<vector<int>> res;
vector<int> vis;
vector<vector<int>> findSubsequences(vector<int>& nums) {
vis.resize(nums.size(),0);
vector<int> path;
dfs(nums,path,0);
return res;
}
void dfs(vector<int> & nums, vector<int>& path, int start){//参数表示状态,从前往后,depth改为start
if(path.size() >= 2){//过程状态也要记录
res.push_back(path);
}
if(start == nums.size()) return;
unordered_set<int> mp;//去重
for(int i = start;i < nums.size(); i++){//扩展方式
//if(vis[i] == 0){这里不需要vis
if((path.size() == 0 || nums[i] >= path.back()) && mp.count(nums[i]) == 0){
mp.insert(nums[i]);
path.push_back(nums[i]);
dfs(nums,path,i + 1);//这里别写错了
path.pop_back();
}
}
}
};

39. 组合总和

元素可以重复利用且没有顺序,所以不要vis数组

class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> combinationSum(vector<int>& nums, int target) {
vector<int> path;
dfs(nums,path,0,target);
return res;
}
void dfs(vector<int> &nums,vector<int>& path, int start, int target){
//出口
if(target < 0) return;
if(target == 0){
res.push_back(path);
return;
}
//遍历
for(int i = start; i <nums.size(); i++){
path.push_back(nums[i]);
dfs(nums,path,i, target - nums[i]);
path.pop_back();
}
}
};

40. 组合总和 II 比上题多了一个去重操作;

class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> combinationSum2(vector<int>& nums, int target) {
sort(nums.begin(),nums.end());//加个去重操作
vector<int> path;
dfs(nums,path,0,target);
return res;
}
void dfs(vector<int> &nums,vector<int>& path, int start, int target){
//出口
if(target < 0) return;
if(target == 0){
res.push_back(path);
return;
}
//遍历
for(int i = start; i <nums.size(); i++){
if(i > start && nums[i] == nums[i-1] ) continue;//注意是i > start
path.push_back(nums[i]);
dfs(nums,path,i+1, target - nums[i]); // start = i + 1
path.pop_back();
}
}
};

78. 子集

class Solution {
public:
vector<vector<int>> res;
vector<int> vis;
vector<vector<int>> subsets(vector<int>& nums) {
vis.resize(nums.size(),0);
vector<int> path;
dfs(nums,path,0);
return res;
}
void dfs(vector<int> & nums, vector<int>& path, int start){//参数表示状态,从前往后,depth改为start
res.push_back(path);
if(start == nums.size()) return;
for(int i = start;i < nums.size(); i++){//扩展方式
//似乎没条件
path.push_back(nums[i]);
dfs(nums,path,i + 1);//这里别写错了
path.pop_back();
}
}
};

90. 子集 II 上题基础上加个去重

class Solution {
public:
vector<vector<int>> res;
vector<int> vis;
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());//排序便于去重
vis.resize(nums.size(),0);
vector<int> path;
dfs(nums,path,0);
return res;
}
void dfs(vector<int> & nums, vector<int>& path, int start){//参数表示状态,从前往后,depth改为start
res.push_back(path);
if(start == nums.size()) return;
for(int i = start;i < nums.size(); i++){//扩展方式
if(i > start && nums[i] == nums[i-1]) continue; //去重
path.push_back(nums[i]);
dfs(nums,path,i + 1);//这里别写错了
path.pop_back();
}
}
};

进阶:N皇后问题

51. N皇后

class Solution {
public:
vector<vector<string>> res;
vector<vector<string>> solveNQueens(int n) {
string t = "";
for(int i = 0; i < n; i++) t += '.';
vector<string> path(n,t);
vector<vector<int>> vis(n, vector<int>(n,0));
dfs(path,vis,n,0);
return res;
}
void dfs(vector<string>& path,vector<vector<int>> &vis, int& n, int row){
if(row == n){//递归出口,前n行全部被赋值
res.push_back(path);return;
}
for(int j = 0; j < n; j++){//遍历一行
if(vis[row][j] == 0){//条件
path[row][j] = 'Q';
//注意,不能直接让vis变1和变0,否则后来的修改可能会改变原来的修改
for(int i = row; i < n; i++)vis[i][j]++;//该列
for(int i = row; i < n; i++){//对角线
if(j + i - row < n) vis[i][j + i - row]++;
if(j + row - i >= 0) vis[i][j + row - i]++;
}
dfs(path, vis, n, row + 1);
path[row][j] = '.';
for(int i = row; i < n; i++)vis[i][j]--;//该列
for(int i = row; i < n; i++){//对角线
if(j + i - row < n) vis[i][j + i - row]--;
if(j + row - i >= 0) vis[i][j + row - i]--;
}
}
}
}
};

37. 解数独

class Solution {
public:
int col[9][9] = {0};
int row[9][9] = {0};
int box[9][9] = {0};
void solveSudoku(vector<vector<char>>& board) {
int n = board.size();
//初始化
for(int i = 0; i <9; i++)
for(int j = 0; j < 9; j++){
if(board[i][j] != '.'){
int t = board[i][j] - '1';
col[j][t]++;
row[i][t]++;
box[i/3*3+ j/3][t]++;
}
}
dfs(board, 0, 0);
}
//传参 int a[][9] 或者 int (*a)[9] 而不是 (int*)[9] a
// box x/3*3 + y/3 而不是 x/3 + y/3;
// (y + 1)%3 而不是 (++y)/3
bool dfs(vector<vector<char>>& board, int x, int y){
if(x == 9) return true;//递归出口
//特殊状态
if(board[x][y] != '.') return dfs(board,x + (y==8),(++y)%9);
for(int k = 0; k < 9; k++){//遍历
//条件
if(row[x][k] || col[y][k] || box[x/3*3 + y/3][k]) continue;
board[x][y] = k + '1';
row[x][k]++;col[y][k]++;box[x/3*3 + y/3][k]++;
if(dfs(board,x + (y==8),(y + 1)%9)) return true;//找到一个解就退出
board[x][y] = '.';
row[x][k]--;col[y][k]--;box[x/3*3 + y/3][k]--;
}
return false;
}
};

dfs-入门模板的更多相关文章

  1. 算法学习之BFS、DFS入门

    算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...

  2. HDU 1251 统计难题(字典树入门模板题 很重要)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  3. 母函数(Generation Function) 入门 + 模板

    转自:母函数 入门 + 模板  感谢 在数学中,某个序列的母函数(Generating function,又称生成函数)是一种形式幂级数,其每一项的系数可以提供关于这个序列的信息.使用母函数解决问题的 ...

  4. DFS 算法模板

    dfs算法模板: 1.下一层是多节点的dfs遍历 def dfs(array or root, cur_layer, path, result): if cur_layer == len(array) ...

  5. DFS算法(——模板习题与总结)

    首先,需要说明的是搜索算法本质上也是枚举的一种,时间复杂度还是很高的,遇到问题(特别是有水平的比赛上),不要优先使用搜索算法. 这里总结一下DFS算法: 1.从图中某个顶点出发,访问v. 2.找出刚访 ...

  6. Oil Deposits(poj 1526 DFS入门题)

    http://poj.org/problem?id=1562                                                                       ...

  7. 网络流之最大流与最小费用流入门&&模板

    理解处 刷题处 模板处 最大流模板 处理重边的+(优化) #include<bits/stdc++.h> using namespace std; ; const int INF = 0x ...

  8. hdu 3549 Flow Problem【最大流增广路入门模板题】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...

  9. 图的dfs遍历模板(邻接表和邻接矩阵存储)

    我们做算法题的目的是解决问题,完成任务,而不是创造算法,解题的过程是利用算法的过程而不是创造算法的过程,我们不能不能陷入这样的认识误区.而想要快速高效的利用算法解决算法题,积累算法模板就很重要,利用模 ...

  10. hdu:2089 ( 数位dp入门+模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位dp的模板题,统计一个区间内不含62的数字个数和不含4的数字个数,直接拿数位dp的板子敲就行 ...

随机推荐

  1. 区块链特辑——solidity语言基础(三)

    Solidity语法基础学习 五.映射类型: 映射型态 Mapping Type 映射钥匙Key → 真实资料 Value mapping(KeyType → ValueType) VariableN ...

  2. MySQL 判断语句 条件函数 case when、if、ifnull

    在MySQL中,需要用到条件判断函数,例如 case when.if.ifnull. 一.方法分类 二.具体方法 (1)if if(expr,result_true,result_false) 注意: ...

  3. Java集合 Map 集合 与 操作集合的工具类: Collections 的详细说明

    Java集合 Map 集合 与 操作集合的工具类: Collections 的详细说明 每博一文案 别把人生,输给心情 师父说:心情不是人生的全部,却能左右人生的全部. 你有没有体会到,当你心情好的时 ...

  4. 面试必问:说一下 Java 虚拟机的内存布局?

    我们通常所说的 Java 虚拟机(JVM)的内存布局,一般是指 Java 虚拟机的运行时数据区(Runtime Data Area),也就是当字节码被类加载器加载之后的执行区域划分.当然它通常是 JV ...

  5. nexus私服搭建的上传和下载

    下载 方法1 下载应该先在nexus中创建相关的库(宿主库.代理库.仓库组),也可以使用原有的库 随后在pom文件中增加以下代码,即可从私服中下载相关的依赖包(注:依赖包的下载首先基于本地库,本地库没 ...

  6. v-html渲染页面的时候 css样式无效

    感谢: https://www.cnblogs.com/niuxiaoxian/p/9443873.html 当我们用v-html渲染页面的时候会发现样式并没有添加上,如下 复制代码 <temp ...

  7. Spring04-AOP(Debug查看执行流程)

    1 AOP的几个核心技术 AOP-面向切面编程的实现的核心技术:jvm运行期间对字节码进行修改或者动态生成新的字节码文件(asm技术). 2 AOP的几个核心概念 AOP在运行期间我们要对class文 ...

  8. RocketMQ - 生产者原理

    https://rocketmq.apache.org/ Apache RocketMQ是一款开源的.分布式的消息投递与流数据平台.出生自阿里巴巴,在阿里巴巴内部经历了3个版本后,作为Apache 顶 ...

  9. SpringMVC的文件、数据校验(Vaildator、Annotation JSR-303)

    SpringMvc的文件上传下载: 文件上传 单文件上传 1.底层使用的是Apache fileupload组件进行上传的功能,Springmvc 只是对其进行了封装,简化开发, pom.xml &l ...

  10. EPICS Archiver Appliance在Centos7的定制部署

    EPICS Archiver Appliance的定制部署1 EPICS Archiver Appliance的定制部署2 上面两篇是在Centos8下成功,后来又想在Centos7下跑成功,按上面的 ...