基础算法学习--dfs和bfs
2021-06-05 15:04
阅读:700
标签:ret tip over names strong 方法 注意 输出 char
dfs的模板
注意bool判断是否走过这个点并注意回溯的处理。
注意条件判断和边界问题。
//边界判断即剪枝
if(chk()) return;
if(over(BianJie)) return;
if(bool = false)//未搜索过
bool = true;
//赋值或纪录
dfs(n + 1);
//复原赋值即回溯
bool = false;
dfs 小例题
题目
给定一个整数 n,将数字 1~n 排成一排,将会有很多种排列方法。
现在,请你按照字典序将所有的排列方法输出。
ac代码
#include
using namespace std;
const int N = 1e6;
bool chk[N];
int res[N];
int n;
void dfs (int num){
if(num == n){
for(int i = 0; i > n;
dfs(0);
return 0;
}
n皇后问题题解
#include
using namespace std;
const int N = 20;
int n;
char res[N][N];
bool col[N],dg[N],udg[N];
void dfs(int u){
if( u == n ){
for(int i = 0;i >n;
for(int i = 0 ;i
小tips
- 当处在第i列第u行时,正对角线下标为u + i,反对角线为n - u + i;
基础算法学习--dfs和bfs
标签:ret tip over names strong 方法 注意 输出 char
原文地址:https://www.cnblogs.com/Xuuxxi/p/14619465.html
评论
亲,登录后才可以留言!