当前位置:网站首页>leetcode-记忆化深搜/动态规划v2
leetcode-记忆化深搜/动态规划v2
2022-07-24 18:30:00 【林冲风雪山神庙】
leetcode-记忆化深搜/动态规划v1_林冲风雪山神庙的博客-CSDN博客
741. 摘樱桃


我的错误解法
class Solution(object):
def cherryPickup(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
#两个人同时从起点摘樱桃
def dfs(x1,y1,x2,y2):
if x1 >= len(grid) or y1 >= len(grid) or x2 >= len(grid) or y2 >= len(grid):
return float("-inf")
if grid[x1][y1] == -1 or grid[x2][y2] == -1:
return float("-inf")
if (x1,y1) == (x2,y2):
res = grid[x1][y1]
else:
res = grid[x1][y1] + grid[x2][y2]
res += max(dfs(x1+1,y1,x2,y2),dfs(x1,y1+1,x2,y2))
res += max(dfs(x1,y1,x2+1,y2),dfs(x1,y1,x2,y2+1))
return res
return dfs(0,0,0,0)class Solution:
def cherryPickup(self, grid: List[List[int]]) -> int:
@lru_cache(None)
def dfs(x1,y1,x2,y2):
if x1 > len(grid)-1 or x2 > len(grid)-1 or y1 > len(grid[0])-1 or y2 > len(grid[0])-1:
return float('-inf')
if grid[x1][y1] == -1 or grid[x2][y2] == -1:
return float('-inf')
# # 到终点
if x1 == y1 == x2 == y2 == len(grid)-1:
return grid[x1][y1]
res = 0
if (x1,y1) == (x2,y2):
res += grid[x1][y1]
else:
res += grid[x1][y1] + grid[x2][y2]
#从两个点的移动方向中找最大的,两个人同时移动
res += max(dfs(x1+1,y1,x2+1,y2),dfs(x1,y1+1,x2,y2+1),dfs(x1+1,y1,x2,y2+1),dfs(x1,y1+1,x2+1,y2))
return res
res = dfs(0,0,0,0)
if res == float("-inf"):
return 0
return res174. 地下城游戏


边栏推荐
- 排序的几种方式for while 还有sort
- pinia 入门及使用
- undefined reference to H5PTopen
- Pycharm configuring opencv Library
- Generate publickey with der format public key and report an error
- Read zepto source code touch module
- Some buckles
- Three ways of redis cluster
- Tree chain partition board
- jmeter -- prometheus+grafana服务器性能可视化
猜你喜欢

Simulation implementation vector

Oracle EBS form common objects and their relationships

Is the validity period of the root certificate as long as the server SSL certificate?

QT - animation frame

【微信小程序开发】自定义tabBar案例(定制消息99+小红心)

The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered

Typora is still the most beautiful and beautiful document editing artifact of yyds in my heart. I believe you will never abandon it

Typora 它依然是我心中的YYDS 最优美也是颜值最高的文档编辑神器 相信你永远不会抛弃它

关于接口的写法 1链式判读 ?. 2方法执行 (finally)一定会执行

Sword finger offer 21. adjust the array order so that odd numbers precede even numbers
随机推荐
Namespace: cluster environment sharing and isolation
MySQL configuration file
Generate publickey with der format public key and report an error
odoo中的bom理解
Flink operation Hudi data table
Template inheritance and import
9. BOM object?
[record of question brushing] 20. Valid brackets
ES6 cycle filter value
Date function format conversion
Typora is still the most beautiful and beautiful document editing artifact of yyds in my heart. I believe you will never abandon it
JS array method sort() collation parsing
Escape character in JS?
Several sorting methods for while and sort
Pycharm configuring opencv Library
Pytoch's journey 1: linear model
Mysql——》BufferPool相关信息
理解动态计算图,requires_grad、zero_grad
Sword finger offer 21. adjust the array order so that odd numbers precede even numbers
redis集群的三种方式