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. asp存储过程

    dim s_ip,MyComm s_ip=request.ServerVariables("REMOTE_ADDR") Set MyComm = Server.CreateObje ...

  2. 细说:Unicode, UTF-8, UTF-16, UTF-32, UCS-2, UCS-4

    1. Unicode与ISO 10646 全世界很多个国家都在为自己的文字编码,并且互不想通,不同的语言字符编码值相同却代表不同的符号(例如:韩文编码EUC-KR中“한국어”的编码值正好是汉字编码GB ...

  3. [NYIST15]括号匹配(二)(区间dp)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15 经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来.首先初 ...

  4. [HDOJ5521]Meeting(最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 给n个点,m个块.块内点到点之间话费的时间ti.两个人分别从点1和点n出发,问两人是否可以相遇, ...

  5. Searching in a Radius using Postgres[Marked]

    Searching in a Radius using Postgres Creating a GEO application has never been easier. You can have ...

  6. nodetree中 前面复选框禁用插件

    nodetree中 前面复选框的去掉插件 extendTreeCheck.js /** * tree方法扩展 * 作者:小雪转中雪 */ $.extend($.fn.tree.methods, { / ...

  7. Tomcat遇到”Error listenerStart”或”Error filterStart”问题且无详细日志时的log配置.

    昨天部署web应用到Tomcat之后,无法成功启动,并且控制台没有详细的错误信息,顶多就两行提示信息,例如:严重: Error listenerStart严重: Context [/lizongbo] ...

  8. mysql中sql语句执行时间

    delimiter // set @d=now(); select * from comment; select timestampdiff(second,@d,now()); delimiter ; ...

  9. js如何判断一个对象是不是Array

    typeof 操作符 对于Function, String, Number ,Undefined 等几种类型的对象来说,他完全可以胜任,但是为Array时 var arr=new Array(&quo ...

  10. 一招解决OpenERP8.0安装旧版模块报错

    有喜欢尝鲜的网友开始玩8.0了,可是版本还没发布,社区的很多特别好的模块还没有升级到8,所以经常碰到模块无法安装的问题. No module name osv 网友提出将模块的 from osv im ...