http://www.practice.geeksforgeeks.org/problem-page.php?pid=91

Minimum Points To Reach Destination

Given a grid with each cell consisting of positive, negative or no points i.e, zero points. We can move across a cell only if we have positive points ( > 0 ). Whenever we pass through a cell, points in that cell are added to our overall points. We need to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
 
1.From a cell (i, j) we can move to (i+1, j) or (i, j+1).
2.We cannot move from (i, j) if your overall points at (i, j) is <= 0.
3.We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
 
Example:
 
Input: points[m][n] = { {-2, -3,   3},  
                        {-5, -10,  1},  
                        {10,  30, -5}  
                      };
Output: 7
Explanation:  
7 is the minimum value to reach destination with  
positive throughout the path. Below is the path.
 
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
 
We start from (0, 0) with 7, we reach(0, 1)  
with 5, (0, 2) with 2, (1, 2) with 5, (2, 2)
with and finally we have 1 point (we needed  
greater than 0 points at the end).

Input:

The first line contains an integer 'T' denoting the total number of test cases.
In each test cases, the first line contains two integer 'R' and 'C' denoting the number of rows and column of array.  
The second line contains the value of the array i.e the grid, in a single line separated by spaces in row major order.

Output:

Print the minimum initial points to reach the bottom right most cell in a separate line.

Constraints:

1 ≤ T ≤ 30
1 ≤ R,C ≤ 10
-30 ≤ A[R][C] ≤ 30

Example:

Input:
1
3 3
-2 -3 3 -5 -10 1 10 30 -5
Output:
7

import java.util.*;
import java.lang.*;
import java.io.*; class GFG { public static int func(int[][] arr) { int r = arr.length, c = arr[0].length;
int[][] dp = new int[r][c]; dp[r-1][c-1] = (1 + arr[r-1][c-1] <= 0)? 1: (1 + arr[r-1][c-1]);
for(int j=c-2; j>=0; --j) {
dp[r-1][j] = (dp[r-1][j+1] + arr[r-1][j] <= 0)? 1: (dp[r-1][j+1] + arr[r-1][j]);
} for(int i=r-2; i>=0; --i) {
dp[i][c-1] = (dp[i+1][c-1] + arr[i][c-1] <= 0)? 1: (dp[i+1][c-1] + arr[i][c-1]);
} for(int i=r-2; i>=0; --i) {
for(int j=c-2; j>=0; --j) {
int mmin = Integer.MAX_VALUE;
if(dp[i+1][j] + arr[i][j] <= 0 || dp[i][j+1] + arr[i][j] <= 0) mmin = 1;
else mmin = Math.min(mmin, Math.min(dp[i+1][j], dp[i][j+1]) + arr[i][j]);
dp[i][j] = mmin;
}
} return dp[0][0];
} public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int times = in.nextInt(); while(times > 0) {
--times; int r = in.nextInt(), c = in.nextInt();
int[][] arr = new int[r][c];
for(int i=0; i<r; ++i) {
for(int j=0; j<c; ++j) {
arr[i][j] = in.nextInt();
arr[i][j] *= -1;
}
} System.out.println(func(arr));
}
}
}

geeksforgeeks@ Minimum Points To Reach Destination (Dynamic Programming)的更多相关文章

  1. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  2. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. [Optimization] Advanced Dynamic programming

    这里主要是较为详细地理解动态规划的思想,思考一些高质量的案例,同时也响应如下这么一句口号: “迭代(regression)是人,递归(recursion)是神!” Video series for D ...

  4. Algo: Dynamic programming

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  5. 算法导论学习-Dynamic Programming

    转载自:http://blog.csdn.net/speedme/article/details/24231197 1. 什么是动态规划 ------------------------------- ...

  6. Dynamic Programming: From novice to advanced

    作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...

  7. HDU-4972 A simple dynamic programming problem

    http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...

  8. 70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  9. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. centos 升级GCC/G++

    #get rep yum install centos-release-scl-rh #yum install centos-release-scl # install g++ 5.2.1 yum - ...

  2. bzoj4199

    看到这题我就伤心,当初想到了正解却因为各种sb原因没有写…… 好吧,其实我的正解是比较挫的…… 大家似乎都用了后缀数组,我用了后缀自动机(后缀树) 其实SAM是很好想得,用SAM建出后缀树后 我们考虑 ...

  3. linux 2.6up的设备和设备驱动模型

    在linux2.6 的设备和设备驱动模型构架中,所有的外部设备和驱动程序都挂在总线上 ,总线分为(usb   -- USB设备,PCI  -- PCI 设备 platform --   直接和处理器进 ...

  4. core--作业

    线程被封装在进程中,进程能不能被封装? 当有多个进程协调一起来完成一项任务的时候,就使用"作业"来完成 作业将进程组合在一起,并创建一个"盒子"来限制进程能够做 ...

  5. .gitignore规则不生效的解决办法

    .gitignore规则不生效的解决办法 使用git 的时候,在.gitignore中已经添加了某个文件或者文件夹,但是使用git status还能看见该文件的修改提示--–说明.gitignore未 ...

  6. short s1 = 1; s1 = s1 + 1;和 s1 += 1;

    对于short s1 = 1; s1 = s1 + 1; 由于s1+1运算时会自动提升表达式的类型,所以结果是int型,再赋值给short类型s1时,编译器将报告需要强制转换类型的错误. 对于shor ...

  7. UVa 699 (二叉树) The Falling Leaves

    题意: 按先序方式输入一棵二叉树,节点是带权的,左孩子在父节点的左一个单位,右孩子在父节点的右一个单位,从左到右输出相同水平位置节点之和. 分析: 做了好几道二叉树的题,代码应该也很好理解了.这里ma ...

  8. I.MX6 ov5640 camera

    /************************************************************************ * I.MX6 ov5640 camera * 说明 ...

  9. 【英语】Bingo口语笔记(49) - 春节请客吃饭的表达

  10. 【英语】Bingo口语笔记(57) - 常见的口语弱读