当前位置: 首页 > news >正文

(nice!!!)(LeetCode 每日一题) 808. 分汤 (深度优先搜索dfs)

题目:808. 分汤

在这里插入图片描述
在这里插入图片描述

思路:深度优先搜索dfs+记忆化搜索,时间复杂度0(n*n)。这里的n最大为400。

C++版本:

class Solution {
public:double dfs(int x,int y,vector<vector<double>> &sta){// 平手if(x<=0 && y<=0) return 0.5;// A先耗尽if(x<=0) return 1;// B先耗尽if(y<=0) return 0;// 之前遍历过if(sta[x][y]!=0) return sta[x][y];// 记忆化sta[x][y]=0.25*(dfs(x-4,y,sta)+dfs(x-3,y-1,sta)+dfs(x-2,y-2,sta)+dfs(x-1,y-3,sta));return sta[x][y];}double soupServings(int n) {// 大于10000为1可以传n为10000进去,输出的结果ans与1的误差小于10^-5if(n>=10000) return 1;// 都是25的整数倍,在记忆化时,很多位置都没有用到,所以都除以25n=(n+24)/25;// 记忆化数组,避免重复遍历vector<vector<double>> sta(n+1,vector<double>(n+1));// 深度优先搜索dfsreturn dfs(n,n,sta);}
};

JAVA版本:

class Solution {double dfs(int x,int y,double[][] sta){if(x<=0 && y<=0) return 0.5;if(x<=0) return 1;if(y<=0) return 0;if(sta[x][y]!=0) return sta[x][y];sta[x][y]=0.25*(dfs(x-4,y,sta)+dfs(x-3,y-1,sta)+dfs(x-2,y-2,sta)+dfs(x-1,y-3,sta));return sta[x][y];}public double soupServings(int n) {if(n>=10000) return 1;n=(n+24)/25;double[][] sta=new double[n+1][n+1];return dfs(n,n,sta);}
}

GO版本:

func soupServings(n int) float64 {if n>10000  {return 1}n=(n+24)/25sta:=make([][]float64,n+1)for i:=range sta {sta[i]=make([]float64,n+1)}return dfs(n,n,sta)
}func dfs(x,y int,sta [][]float64) float64 {if x<=0&&y<=0 {return 0.5}if x<=0 {return 1}if y<=0 {return 0}if sta[x][y]!=0 {return sta[x][y]}sta[x][y]=0.25*(dfs(x-4,y,sta)+dfs(x-3,y-1,sta)+dfs(x-2,y-2,sta)+dfs(x-1,y-3,sta))return sta[x][y]
}
http://www.dtcms.com/a/320639.html

相关文章:

  • Latex中公式部分输入正体的字母\mathrm{c}
  • [激光原理与应用-183]:测量仪器 - 光束型 - 光束参数乘积(BPP)的本质与含义,聚焦能力与传输稳定性的物理矛盾。
  • 汽车零部件深孔加工质控升级:新启航激光频率梳 3D 测量解决传统光学扫描遮挡
  • Linux网络--2、Socket编程
  • 力扣-238.除自身以外数组的乘积
  • 《Leetcode》-面试题-hot100-链表
  • 力扣热题100------287.寻找重复数
  • 大语言模型提示工程与应用:提示词基础使用方式
  • 9.2 通过DuEDrawingControl把eDrawing嵌入到C#中显示
  • windows线程基础
  • leetcode热题——有效的括号
  • FPS游戏准心跳动效果实现指南
  • 为什么通过CreateThread创建的线程调用C/C++运行库函数不稳定
  • Sum of Four Values(sorting and searching)
  • 力扣-438.找到字符串中所有字母异位词
  • I/O原理与服务。
  • KTH7111-离轴专用芯片,支持自校准,可替MA600和TLE5012,离轴精度可达±0.2
  • Agent配置最佳实践:Prompt工程与参数调优
  • RAG初步实战:从 PDF 到问答:我的第一个轻量级 RAG 系统(附详细项目代码内容与说明)
  • WWDC 25 玻璃态星际联盟:SwiftUI 视图协同“防御协议”
  • 生产管理ERP系统|物联及生产管理ERP系统|基于SprinBoot+vue的制造装备物联及生产管理ERP系统设计与实现(源码+数据库+文档)
  • 2025华数杯数学建模A题【 多孔膜光反射性能的优化与控制】原创论文分享
  • 暴力解决MySQL连接失败
  • 应用层自定义协议
  • Spring系列之Spring AI入门
  • 关于“致命错误:‘https://github.com/....git/‘ 鉴权失败”
  • 基于Vue.js和Golang构建高效在线客服系统:前端实现与后端交互详解
  • Linux学习-数据结构(哈希表)
  • K8s 常见故障案例分析
  • Axure基于中继器实现的组件库(导航菜单、动态表格)