算是一些套路
1、要定义节点记录信息
typedef struct Node{
int x,y... ;
int step ;
}Node;
搜索树必定要有一个变量作为层数,用step来表示
比如若是背包问题那么就定义
int价值val和重量w
若是迷宫问题
int坐标x和y
如下面两道例题
若是整数变换则每个节点都有各自的值,则int x
若是需要记录对应的路径,就定义char path[N]
2、void bfs之内的套路
第一步,定义根节点和队列并且把根节点压入队列
queue<Node> Q ;
Node first ;
first内的变量赋予初值
Q.push(first) ;
第二步,保持队列非空
while( !Q.empty() )
{
}
第三步,取出队首元素并且弹出队首元素
Node cur=Q.front();
Q.pop();
第四步,设计最终状态
看情况判断
第五步,设计下一步怎么走,用next来命名下一步节点,压入队尾
next=cur;
next.step+=1;
其他视情况而定
//利用分支界限法实现01背包问题
#include
#include
#include
using namespace std;
//定义结点记录信息
typedef struct Node{
int val , w ;
int step ;
}Node;
int weight[3] = {
16 , 15 , 15 };
int value[3] = {
45 , 25 , 25 };
int V = 30 ;
// BFS套路
void BFS(){
int n = 3 ; // 物品个数
int ans = 0 ; // 答案
//第一步:定义根节点和队列,并把根节点压入队列
queue<Node> Q ;
Node first ;
first.step = first.val = first.w = 0 ;
Q.push(first) ;
//第二步:保持队列非空
while( !Q.empty() ){
//第三步:取出队首元素,别忘了同时要弹出队首元素
Node cur = Q.front() , Next ;
Q.pop() ;
//第四步:设计 最终状态 进行的操作 <=> 到达叶子结点.
if( cur.step == n ){
ans = max( ans , cur.val );
continue ;
}
//第五步:设计 下一步怎么走,用"Next"来命名下一步结点,压入队尾
Next = cur ;
Next.step += 1 ;
//该操作为:物品不放进背包 <=> 子集树往左边走
Q.push( Next );
//该操作为:物品放入背包 <=> 子集树往右边走
//前提是:保证物品能放入背包
if( Next.w + weight[ cur.step ] <= V ){
Next.w += weight[ cur.step ];
Next.val += value[ cur.step ];
Q.push( Next ) ;
}
}
printf("%d\n",ans);
}
int main()
{
BFS();
return 0;
}
#include
#include
#include
#define n 8
using namespace std;
int arr[n][n] = {
0, 0, -1, 0, 0, 0, 0, 0,
0, 0, -1, -1, 0, 0, 0, 0,
0, -1, 0, 0, -1, 0, 0, 0,
0, 0, 0, -1, -1, 0, 0, 0,
0, 0, 0, 0, -1, 0, 0, 0,
-1, 0, 0, 0, 0, 0, 0, 0,
-1, -1, -1, 0, 0, 0, 0, 0,
-1, -1, -1, 0, 0, 0, 0, 0 };
typedef struct {
int x, y;
int index;
}node;
int main(){
//方向数组
int offset[4][2] = {
{
-1, 0 },
{
0, -1 }, {
0, 1 },
{
1,0}
};
int tx = 3, ty = 6, bx = 2, by = 1;
queue<node> Q;
node n1, n2;
int index = 1;
n1.x = bx; n1.y = by; n1.index = 0;
Q.push(n1);
while (!Q.empty()){
n1 = Q.front(); Q.pop();
//定义最终情况
if (n1.x == tx && n1.y == ty)
break;
for (int i = 0; i<4; i++){
n2.x = n1.x + offset[i][0];
n2.y = n1.y + offset[i][1];
n2.index = n1.index + 1;
if (n2.x >= 0 && n2.x<n && n2.y >= 0 && n2.y<n){
if (arr[n2.x][n2.y] == 0){
arr[n2.x][n2.y] = n1.index + 1;
Q.push(n2);
}
}
}
}
//打印输出
for (int i = 0; i<n; i++)
{
for (int j = 0; j<n; j++)
printf("%3d", arr[i][j]);
putchar('\n');
}
return 0;
}
相较于BFS来说,DFS搜索顺序为“找到一个节点一直搜索到叶子结点,到了叶子再回头”
#include
#include
#include
using namespace std;
const int N = 5;
int weight[] = {
16, 15, 15 };
int value[] = {
45, 25, 25 };
int v = 30;
int val, w;
int ans;//答案
int n = 3;//物品数量
int vis[N];//物品标记否
void dfs(int step){
//到达叶子节点
if (step == n){
ans = max(ans, val);
return;
}
//物品不在背包中,且放物品后还在背包承受范围内
if (vis[step] == 0 && w + weight[step] <= v){
val += value[step];
w += weight[step];
vis[step] = 1;
//往左子树走
dfs(step + 1);
//回溯,返回结点后需要把第step个物品取出来,
//同时恢复 结点对应的状态
val -= value[step];
w -= weight[step];
vis[step] = 0;
}
//往右子树走
dfs(step + 1);
}
int main(){
dfs(0);
printf("%d", ans);
return 0;
}
逢店加一倍,遇花喝一斗。 这一路上,他一共遇到店5次,遇到花10次,已知最后一次遇到的是花,他正好把酒喝光了。 请你计算李白遇到店和花的次序,可以把遇店记为a,遇花记为b。则:babaabbabbabbbb 就是合理的次序。像这样的答案一共有多少呢?请你计算出所有可能方案的个数
#include
#include
#include
using namespace std;
int cnt = 0;
char path[20];
void dfs(int step,int shop,int flower,int wine){
if (step == 15 && shop == 5 && flower == 10 && wine == 0){
for (int i = 0; i < step; i++){
printf("%c", path[i]);
}
putchar('\n');
cnt++;
return;
}
// 若走左子树,已遇到酒店的数量不超过5,同时过程中不能出现酒为0
if (shop < 5 && wine >= 1){
path[step] = 'a';
dfs(step + 1, shop + 1, flower, wine * 2);
}
// 若走右子树,已遇到花的数量不超过10, 同时酒壶也必须有酒
if (flower < 10 && wine >= 1){
path[step] = 'b';
dfs(step + 1, shop, flower + 1, wine - 1);
}
}
八皇后问题
#include
#include
const int N = 50 ;
int vis[N] , Queen[N] ;
//判断放置在x列的皇后是否合法
bool check( int x ){
for( int i = 1 ; i < x ; i++ ){
if( abs( x - i ) == abs( Queen[x] - Queen[i] ) ){
return false ;
}
}
return true ;
}
int n = 8 ;
int ans = 0 ;
void dfs( int step ){
if( step == n + 1 ){
ans ++ ;
return ;
}
for( int i = 1 ; i <= n ; i++ ){
//当前行没被占据
if( vis[i] == 0 ){
//把皇后 放置在 <第i行,第step列>
Queen[step] = i ;
//判断其是否合法,若合法继续搜索
if( check(step) ){
//往下深搜时,记得打标记.
vis[i] = 1 ;
dfs( step + 1 );
//回溯使记得撤标.
vis[i] = 0 ;
}
}
}
}
int main()
{
dfs( 1 ) ;
printf("%d\n",ans);
return 0;
}
这是规定第一个数为1的
全排列的变种问题,因为其实就是在过程中加了判断,判断当前要填的数与前一个数之和是否为素数,然后最后要判断最后一个数与第一个的1的和是否为素数
#include
#include
using namespace std;
const int N = 1e3 + 10;
int n;
int a[N];
int vis[N];
bool is_prime[N];
//初始化 判断在一个很大的范围内的数是否为素数
void Init(){
for (int i = 2; i < N; i++) {
bool f = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
f = false;
break;
}
}
if (f)
is_prime[i] = true;
}
//把第一个值固定好为1
a[1] = 1; vis[1] = -1;
}
void dfs_subset(int step){
//最后一步 判断最后一个值与1的和是否为素数,如是那就输出一种情况
if (step == n + 1){
if (is_prime[a[n] + 1]){
for (int i = 1; i <= n; i++){
printf("%d%c", a[i], i == n ? '\n' : ' ');
}
}
return;
}
//从第二个开始判断与前一个之和是不是素数
for (int i = 2; i <= n; i++) {
if (vis[i] == 0 && is_prime[i + a[step - 1]]) {
vis[i] = 1; a[step] = i;
dfs_subset(step + 1);
vis[i] = 0;
}
}
}
#include
const int N = 20;
const int M = 5;
int magic[M][M], n = 4;
int row[M], col[M], diag[2];
int vis[N];
void Init(){
magic[1][1] = 16; magic[1][4] = 13;
magic[2][3] = 11; magic[3][1] = 9;
magic[4][2] = 15; magic[4][4] = 1;
//vis[*] = -1 固定着,不让其修改
vis[1] = vis[9] = vis[11] = vis[13] = vis[15] = vis[16] = -1;
//设定行列对角线的初始值
row[1] = 29; row[2] = 11; row[3] = 9; row[4] = 16;
col[1] = 25; col[2] = 15; col[3] = 11; col[4] = 14;
diag[0] = 17; diag[1] = 24;
}
//遍历顺序是从左往右,从上到下
void dfs(int x, int y){
//到达最后一个格子,因为最后一个位置已固定,所以直接计算即可
if (x == n && y == n){
int tmp = 0;
for (int i = 1; i <= n; i++){
tmp += (row[i] == 34);
tmp += (col[i] == 34);
}
tmp += (diag[0] == diag[1] && diag[0] == 34);
if (tmp == 9){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
printf("%-3d", magic[i][j]);
}
putchar('\n');
}
}
return;
}
if (vis[magic[x][y]] == -1){
if (y == n){
dfs(x + 1, 1);
}
else{
dfs(x, y + 1);
}
}
else{
//因为1,15,16都已在幻方中,所以直接搜索[2,14]
for (int i = 2; i <= 14; i++){
//当前这个数字没有被用过
if (vis[i] == 0){
vis[i] = 1;
magic[x][y] = i;
//添加后修改该行列对角线的总和
row[x] += i;
col[y] += i;
diag[0] += (x == y) * (i);
diag[1] += (x == n + 1 - y) * (i);
//往下走,如果到最右边,则换行
if (y == n){
dfs(x + 1, 1);
}
else{
dfs(x, y + 1);
}
//记得撤出标记
vis[i] = 0;
row[x] -= i;
col[y] -= i;
diag[0] -= (x == y) * (i);
diag[1] -= (x == n + 1 - y) * (i);
}
}
}
}
int main(){
Init();
dfs(1, 1);
return 0;
}