码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【英雄哥六月集训】第 27天: 图


    系列文章

    【英雄哥六月集训】第 01天:数组

    【英雄哥六月集训】第 02天:字符串

    【英雄哥六月集训】第 03天:排序

    【英雄哥六月集训】第 04天:贪心

    【英雄哥六月集训】第 05天: 双指针

    【英雄哥六月集训】第 06天:滑动窗口

    【英雄哥六月集训】第 07天:哈希

    【英雄哥六月集训】第 08天:前缀和

    【英雄哥六月集训】第 09天:二分查找

    【英雄哥六月集训】第 10天: 位运算

    【英雄哥六月集训】第 11天: 矩阵

    【英雄哥六月集训】第 12天: 链表

    【英雄哥六月集训】第 13天: 双向链表

    【英雄哥六月集训】第 14天: 栈

    【英雄哥六月集训】第 15天: 二叉树

    【英雄哥六月集训】第 16天: 队列

    【英雄哥六月集训】第 17天: 广度优先搜索

    【英雄哥六月集训】第 18天: 树

    【英雄哥六月集训】第 19天: 二叉树

    【英雄哥六月集训】第 20天: 二叉搜索树

    【英雄哥六月集训】第 21天: 堆(优先队列)

    【英雄哥六月集训】第 22天: 有序集合

    【英雄哥六月集训】第 23天: 字典树

    【英雄哥六月集训】第 24天: 线段树

    【英雄哥六月集训】第 25天: 树状数组

    【英雄哥六月集训】第 26天: 并查集

    【英雄哥六月集训】第 27天: 图

    文章目录

    • 系列文章
    • 图
    • 一、 概率最大的路径
    • 二、 信物传送
    • 三、 T 秒后青蛙的位置
    • 总结


    图

    图通常用来表示和存储具有“多对多”关系的数据,是数据结构中非常重要的一种结构。

    一、 概率最大的路径

    1514. 概率最大的路径

    class Solution {
        int maxn = 10010;
        List<List<Edge>> edge = new ArrayList<List<Edge>>(maxn);
        class Edge{
            int to;
            double val;
            Edge(){
            }
    
            Edge(int t,double v){
                this.to=t;
                this.val=v;
            }
        }
    
        public double bfs(int start,int end){
            double[] dis = new double[maxn];
            int i;
            for(i=0;i<maxn;++i){
                dis[i]=0.00d;
            }
            dis[start]=1.00d;
            Queue<Integer> q = new ArrayDeque<Integer>();
            q.add(start);
            while(!q.isEmpty()){
                Integer u = q.remove();
                int len = edge.get(u).size();
                for(i=0;i<len;++i){
                    int v = edge.get(u).get(i).to;
                    double d = dis[u]*(edge.get(u).get(i).val);
    
                    if(d>dis[v]){
                        dis[v]=d;
                        q.add(v);
                    }
                }
            }
            return dis[end];
        }
        public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
            int i;
            for(i=0;i<maxn;++i){
                edge.add(new ArrayList<Edge>());
            }
            for(i=0;i<edges.length;++i){
                if(succProb[i]<=0){
                    continue;
                }
                int u = edges[i][0];
                int v = edges[i][1];
                double w = succProb[i];
    
                edge.get(u).add(new Edge(v,w));
                edge.get(v).add(new Edge(u,w));
                
            }
            return bfs(start,end);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    二、 信物传送

    LCP 56. 信物传送

    class Solution {
        int[][] dir = new int[][]{
            {-1,0},{0,1},{1,0},{0,-1}
        };
        public int conveyorBelt(String[] matrix, int[] start, int[] end) {
            int i,j;
            int m=matrix.length;
            int n = matrix[0].length();
            int[][] grid = new int [110][110];
            for(i=0;i<m;i++){
                for(j=0;j<n;j++){
                    if(matrix[i].charAt(j)=='^'){
                        grid[i][j]=0;
                    }
                    else if(matrix[i].charAt(j)=='>'){
                        grid[i][j]=1;
                    }
                    else if(matrix[i].charAt(j)=='v'){
                        grid[i][j]=2;
                    }
                    else if(matrix[i].charAt(j)=='<'){
                        grid[i][j]=3;
                    }
                }
            }
            int u = start[0]*n+start[1];
            int[] dis = new int[10010];
            for(i=0;i<n*m;++i){
                dis[i]=100000000;
            }
            dis[u]=0;
            Deque<Integer> q = new ArrayDeque<Integer>();
            q.add(u);
            while(!q.isEmpty()){
                Integer f = q.remove();
                int x = f/n;
                int y = f%n;
                for(i=0;i<4;++i){
                    int tx = x+dir[i][0];
                    int ty = y+dir[i][1];
    
                    if(tx<0||ty<0||tx==m||ty==n){
                        continue;
                    }
                    int next = tx*n+ty;
                    int nextd = dis[f]+((grid[x][y]==i?0:1));
                    if(nextd < dis[next]){
                        dis[next] = nextd;
                        q.add(next);
                    }
                }
            }
            return dis[end[0]*n+end[1]];
    
            
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    三、 T 秒后青蛙的位置

    1377. T 秒后青蛙的位置

    class Solution {
        int maxn = 110;
        List<List<Integer>> e = new ArrayList<List<Integer>>(maxn);
        double ans = 0.00d;
        public void inite(){
            for(int i=0;i<maxn;i++){
                e.add(new ArrayList<Integer>());
            }
        }
        public void dfs(int u,int fat,double p,int step,int maxstep,int target){
            int i,cnt=0;
            for(i =0; i<e.get(u).size(); ++i){
                if(e.get(u).get(i)!=fat){
                    ++cnt;
                }
            }
            if(step <= maxstep){
                if(u==target){
                    if(cnt ==0 && step <= maxstep){
                        ans = p;
                    } else if(cnt>=1 && step == maxstep){
                        ans = p;
                    }
                }
            }
            if(step == maxstep){
                return ;
            }
    
            for(i=0; i<e.get(u).size(); ++i){
                if(e.get(u).get(i)!=fat){
                    dfs(e.get(u).get(i),u,p*1.0/cnt,step+1,maxstep,target);
                }
            }
        }
        public double frogPosition(int n, int[][] edges, int t, int target) {
            inite();
            int i;
            for(i=0; i<edges.length; ++i){
                int u = edges[i][0];
                int v = edges[i][1];
    
                e.get(u).add(v);
                e.get(v).add(u);
            }
    
            dfs(1,0,1,0,t,target);
            return ans;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    总结

  • 相关阅读:
    linux sed命令使用说明
    一元二次函数教案
    SpringBoot SpringBoot 基础篇(第一篇) 第2章 SpringBoot 全局配置 2.3 yaml 读取数据
    隐式渲染 收集 SphereTracing
    angular升级后用Cesium等编译出现Can‘t resolve (fs, http, https, url, path, stream, zlib)等问题
    Python爬虫教程:解析网页中的元素
    华为开源自研AI框架昇思MindSpore应用案例:消噪的Diffusion扩散模型
    【Vue】首屏加载优化
    C++内存泄漏排查以及几个工具
    深入理解MySQL索引的数据结构和事务的四大特性、隔离性的四种级别
  • 原文地址:https://blog.csdn.net/weixin_39348931/article/details/125488836
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号