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. Android内存管理(1)WRANGLING DALVIK: MEMORY MANAGEMENT IN ANDROID PART 1

    from : http://www.raizlabs.com/dev/2014/03/wrangling-dalvik-memory-management-in-android-part-1-of-2 ...

  2. [HIHO1223]不等式(离散化,枚举)

    题目链接:http://hihocoder.com/problemset/problem/1223 这题不难,难点在于小数的处理.可以0.5为步长枚举,也可以扩大偶数倍枚举. /* ━━━━━┒ギリギ ...

  3. hadoop hadoop-0.20.2-cdh3u4升级

    [hadoop@lab02 ~]$ uname -aLinux lab02 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x86_ ...

  4. C#生成图形验证码

    先看效果: 再上代码 public class CaptchaHelper { private static Random rand = new Random(); private static in ...

  5. UVa 10474 Where is the Marble

    题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...

  6. mongodb主从复制

    1)主服务器--master --port 20001 2)从服务器--slave --source 127.0.0.1:20001 --port 20002 注释:--master 以主服务器形式启 ...

  7. ORACLE解锁record is locked by another user

    在操作ORACLE数据库的时候,由于执行完,没有COMMIT,直接把PL/SQL关闭掉,后来导致那张表被锁住,当编辑时就会出现这个信息,record is locked by another user ...

  8. python - 简明 性能测试

    简洁测试: # python -m cProfile test.py 代码注入: # -*- coding: utf-8 -*- class test(object): pass class test ...

  9. win7下的IP-主机名映射

    今天学了个技巧,win7下有个目录:C:\Windows\System32\drivers\etc 该目录下有个文件: hosts 在这个文件里面我们可以映射IP-主机名: 127.0.0.1 loc ...

  10. MAC OSX 下安装Cscope

    续前文,搞定CTAGS之后,需要被搞定的是cscope,依旧是上网拖一把,具体过程如下   #1 下载cscope最新版本 http://cscope.sourceforge.net/#downloa ...