jumpgame
来源:互联网 发布:个人博客网站源码 编辑:程序博客网 时间:2024/05/10 08:22
动态规划经典题目
用“局部最优和全局最优解法”。我们维护一个到目前为止能跳到的最远距离,以及从当前一步出发能跳到的最远距离。局部最优local=A[i]+i,而全局最优则是global=Math.max(global, local)。递推式出来了,代码就比较容易实现了。因为只需要一次遍历时间复杂度是O(n),而空间上是O(1)。代码如下:
include
include
include
include
include
using namespace std;
int n, board[100][100];
int cache[100][100];
int jump2(int x, int y)
{
if (y >= n || x >= n)
return 0;
if (y == n-1 && x == n -1)
return 1;
int& ret = cache[x][y];//这里是引用的意思,而且ret的值改变了,cache[x][y]的值也会发生改变
if (ret != -1)
return ret;
int jumpSize = board[x][y];
return ret = (jump2(x+jumpSize,y)|| jump2(x,y+jumpSize));//加步数
}
int main()
{
cin>>n;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cin>>board[i][j];
}
}
memset(cache,-1,sizeof(cache));
int flag = 0;
flag = jump2(0,0);
if (flag)
cout<<”yes”<
- jumpgame
- leetcode JumpGame
- leetcode--JumpGame
- leetcode--JumpGame
- leetcode : jumpgame
- JumpGame II
- LeetCode JumpGame and JumpGame II
- leetcode jumpgame 2
- leetcode解题方案--055--jumpGame
- LeetCode No.45 JumpGame 2 题解
- 一键生成Android/Android-HD/IOS 多尺寸ICON
- 核心Dom节点访问
- Java设计模式之创建型模式--原型模式
- static成员在JVM中的存储
- 动态规划实例(十三):最长公共子串(LCS)
- jumpgame
- 顺序图
- libigl安装
- 0CTF-EasiestPrintf真的很easy
- Spark SQL RDD与DataFrames相互转换
- IOS Storyboard页面切换详解
- 函数参数的int &和unsigned int &没法兼容
- SDUT2449-->走迷宫
- (6/16)JSP过滤器