当前位置:网站首页>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. 地下城游戏


边栏推荐
- The collapse of margin
- Template inheritance and import
- JS array method sort() collation parsing
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
- 怎么解决idea中yaml无法识别或者飘红?
- Introduction to nipple music theory and personal perception
- 1. Typeof view variable type?
- Custom web framework
- Three ways of redis cluster
- Is the validity period of the root certificate as long as the server SSL certificate?
猜你喜欢

Escape character in JS?

Get familiar with pytoch and pytoch environment configuration

JMeter -- silent operation

pycharm配置opencv库

【OpenCV】—阈值化

Rookie colleagues cost me 2K. Did you recite the secret of salary increase? (collect it quickly!)

模拟实现vector

第五届数字中国建设峰会在福建福州开幕

jmeter --静默运行

Flink operation Hudi data table
随机推荐
模拟实现vector
Ionic4 learning notes 11 - popular goods display of an East Project
Custom web framework
MySQL configuration file
CF lomsat gelral (heuristic merge)
Three ways of redis cluster
Getting started with MySQL database
开窗函数(1)-部门工资前三员工
Get familiar with pytoch and pytoch environment configuration
XSS bypass pose summary
Ionic4 learning notes 3
Several sorting methods for while and sort
How to solve the problem that yaml in idea is unrecognized or red?
无关的表进行关联查询及null=null条件
Simulation implementation vector
空间三点画圆代码
数组扁平化.flat(Infinity)
如何用WebGPU流畅渲染百万级2D物体?
Example of single table query in ORM student management system
4. Basic type and reference type?