geeksforgeeks@ Minimum Points to Reach Destination (Dynamic programming)

Source: Internet
Author: User

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 is added to our overall points. We need to find minimum initial points to reach cells (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 has 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 had 1 point (we needed
Greater than 0 points at the end).

Input:

The first line contains a integer ' T ' denoting the total number of test cases.
In each test cases, the first line contains the number ' 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, with 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

ImportJava.util.*;Importjava.lang.*;ImportJava.io.*;classGFG { Public Static intFuncint[] arr) {                intR = 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(intJ=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(intI=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(intI=r-2; i>=0; --i) { for(intJ=c-2; j>=0; --j) {intMmin =Integer.max_value; if(Dp[i+1][j] + arr[i][j] <= 0 | | dp[i][j+1] + ARR[I][J] <= 0) mmin = 1; ElseMmin = Math.min (Mmin, Math.min (Dp[i+1][j], dp[i][j+1]) +Arr[i][j]); DP[I][J]=mmin; }        }                returnDp[0][0]; }         Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intTimes =In.nextint ();  while(Times > 0) {            --Times ; intR = In.nextint (), C =In.nextint (); int[] arr =New int[R][c];  for(inti=0; i<r; ++i) { for(intj=0; j<c; ++j) {Arr[i][j]=In.nextint (); ARR[I][J]*=-1;        }} System.out.println (func (arr)); }    }}
View Code

[email protected] Minimum Points to Reach Destination (Dynamic programming)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.