7.搜索——2.深度优先搜索DFS——有解还是无解

news/2024/7/20 22:07:13 标签: 深度优先, 算法

深度优先搜索的特色

递归:判断有解还是无解

  1. 返回值设计
    1. 如果只是遍历,返回值void即可
    2. 判断是否有路径,返回值bool
  2. 参数设计:DFS(当前结点信息,终点状态)
  3. 去重,visited集合
  4. 记录路径:栈
bool DFS(current,end){
	stack.push(current);
	visited[current]=true;
	if(current == end){
		return true;
	}
	while(current有未被访问的邻居){
		if(DFS(该邻居,end) == true&&visited[邻居]=false){
			return true;
		}
	}
	visited[current]=false;
	stack.pop();
	return false;
}

例题——A knight’s journey

在这里插入图片描述

代码

#include <cstdio>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
//#include <bits/stdc++.h>
using namespace std;
int direction[8][2]={
        {-1,-2},{1,-2},
        {-2,-1},{2,-1},
        {-2,1},{2,1},
        {-1,2},{1,2}
};
bool visited[50][50]= {false};
bool DFS(int x, int y, int current, int p, int q, string path){
    //把本结点加入路径
    path += (y+'A');
    path += (x+'1');
    //本节点设为已经访问
    visited[x][y]= true;
    //判断是否达到终点
    if (current==p*q){
        printf("%s\n\n",path.c_str());
        return true;
    }

    //遍历邻居
    for (int i = 0; i < 8; ++i) {
        if (x+direction[i][0] >= 0 &&x+direction[i][0]<p&&
            y+direction[i][1] >= 0 &&y+direction[i][1]<q&&
        visited[x+direction[i][0]][y+direction[i][1]]== false){
            if (DFS(x+direction[i][0],y+direction[i][1],current+1,p,q,path)==true){
                //如果下一个结点能够达到,就说明本节点也能达到
                return true;
            }

        }

    }
    //不存在从本节点除法到终点的路径
    visited[x][y]= false;
    path.substr(0,path.size()-2);//删掉最后两个字符
    return false;
}

int main() {
    int n,p,q;
    scanf("%d",&n);
    for (int i = 0; i < n; ++i) {
        scanf("%d%d",&p,&q);
        string path;
        printf("Scenario #%d:\n",i+1);
        for (int j = 0; j < p; ++j) {
            for (int k = 0; k < q; ++k) {
                visited[j][k]= false;
            }
        }
        if (DFS(0,0,1,p,q,path)== false){
            printf("impossible\n\n");
        }
    }
    return 0;
}

例题——Square

在这里插入图片描述

代码

#include <cstdio>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
//#include <bits/stdc++.h>
using namespace std;
int m;
int stick[30];
bool is_used[30];
bool DFS(int current_side, int current_length,int position, int side){
    //current_side  已经拼好的边长个数
    //current_length正在拼的边长的长度
    //position      木棍遍历的起点
    if (current_side==3){
        return true;
    }
    for (int i = position; i < m; ++i) {
        if (is_used[i]==true||current_length+stick[i]>side){
            continue;
        }
        //暂时拿这个木棍拼边
        is_used[i]= true;
        if (current_length+stick[i]<side){
            if (DFS(current_side,current_length+stick[i],i+1,side)==true){
                return true;
            }
        } else if(current_length+stick[i]==side){
            if (DFS(current_side+1,0,0,side)== true){
                return true;
            }
        }
        //取回刚刚的木棍
        is_used[i]= false;
    }
    return false;
}

int main() {
    int n;
    scanf("%d",&n);
    for (int idx = 0; idx < n; ++idx) {
        scanf("%d",&m);
        int total = 0;
        for (int i = 0; i < m; ++i) {
            scanf("%d",&stick[i]);
            total += stick[i];
            is_used[i]= false;
        }
        if (total%4==0&& DFS(0,0,0,total/4)){
            printf("yes\n");
        } else{
            printf("no\n");
        }
    }
    return 0;
}

http://www.niftyadmin.cn/n/5437282.html

相关文章

C++——字符串、读写文件、结构体、枚举

个人简介 &#x1f440;个人主页&#xff1a; 前端杂货铺 &#x1f64b;‍♂️学习方向&#xff1a; 主攻前端方向&#xff0c;正逐渐往全干发展 &#x1f4c3;个人状态&#xff1a; 研发工程师&#xff0c;现效力于中国工业软件事业 &#x1f680;人生格言&#xff1a; 积跬步…

牛客NC358 组合【中等 DFS Java,Go,PHP】

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/7cfd3675cc964ae6818a771ac97ece5f 思考 DFS参考答案Java import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定&#xff0c;请勿修改&#xff0c;直接返回方法规定的值即可*…

共享冷链平台架构——构建高效、可持续的冷链物流体系

随着生鲜产品和医药品等对温度控制要求严格的物品需求不断增加&#xff0c;冷链物流行业逐渐成为物流行业的重要组成部分。共享冷链平台作为一种创新的物流模式&#xff0c;为各行各业提供了便捷、高效的冷链物流服务。本文将深入探讨冷链共享平台的业务与技术架构&#xff0c;…

常用pip命令

pip是一个现代的&#xff0c;通用的Python包管理工具。它提供了对Python包的查找、下载、安装、卸载的功能。 安装库 pip install package_name如果你想从特定的源安装&#xff0c;可以使用-i或--index-url选项&#xff1a; pip install package_name -i https://pypi.examp…

Linux:网络套接字的认识和基本实现通信

文章目录 UDP和TCP协议网络字节序socket编程常见的接口套接字 本篇总结的是对于网络套接字的基本认识 UDP和TCP协议 在谈网络套接字前&#xff0c;必须先对于UDP和TCP这两个协议有一个基本的认识&#xff0c;这两个协议都是隶属于传输层的协议&#xff0c;并且这两个协议距离…

const,static深度总结——c++穿透式分析

前言&#xff1b;c类和对象的知识点中除了几种默认函数&#xff0c; 比较重要的还有使用const和static修饰成员相关知识点。const在c中特性很简单。 但是在使用中&#xff0c; 比较容易疏忽大意出现问题。 static特性也很简单&#xff0c; 但是比起const来要直接的多。 在使用中…

SpringBoot中使用MybatisX插件的详细过程

MybatisX 是一款针对 MyBatis 框架的 IntelliJ IDEA 的快速开发插件&#xff0c;旨在提高 MyBatis 开发效率的工具。它提供了一系列功能来简化 MyBatis 的配置和使用&#xff0c;包括 XML 文件的智能补全、快速跳转、代码生成等功能。 实现细节 &#xff08;1&#xff09;在I…

顶顶通呼叫中心中间件-群集配置方法讲解(mod_cti基于FreeSWITCH)

群集介绍 比较多的外呼或呼入系统&#xff0c;假如整个系统需要1万并发&#xff0c;单机最高就3000-5000并发&#xff0c;这时就需要多机群集了。顶顶通呼叫中心中间件使用redis数据库&#xff0c;多个FreeSWITHC(mod_cti)连接同一个redis就可以很容易的配置成群集系统。 想了…