Problem Statement
One of the secret chambers in Hogwarts is full of philosopher’s stones. The floor of the chamber is covered by h × w square tiles, where there are h rows of tiles from front (first row) to back (last row) and w columns of tiles from left to right. Each tile has 1 to 100 stones on it. Harry has to grab as many philosopher’s stones as possible, subject to the following restrictions:
One of the secret chambers in Hogwarts is full of philosopher’s stones. The floor of the chamber is covered by h × w square tiles, where there are h rows of tiles from front (first row) to back (last row) and w columns of tiles from left to right. Each tile has 1 to 100 stones on it. Harry has to grab as many philosopher’s stones as possible, subject to the following restrictions:
- He starts by choosing any tile in the first row, and collects the philosopher’s stones on that tile. Then, he moves to a tile in the next row, collects the philosopher’s stones on the tile, and so on until he reaches the last row.
- When he moves from one tile to a tile in the next row, he can only move to the tile just below it or diagonally to the left or right.
Input
The first line consists of a single integer T, the number of test cases. In each of the test cases, the first line has two integers. The first integer h (1 <= h <= 100) is the number of rows of tiles on the floor. The second integer w (1 <= w <= 100) is the number of columns of tiles on the floor. Next, there are h lines of inputs. The i-th line of these, specifies the number of philosopher’s stones in each tile of the i-th row from the front. Each line has w integers, where each integer m (0 <= m <= 100) is the number of philosopher’s stones on that tile. The integers are separated by a space character.Output
The output should consist of T lines, (1 <= T <= 100), one for each test case. Each line consists of a single integer, which is the maximum possible number of philosopher’s stones Harry can grab, in one single trip from the first row to the last row for the corresponding test case.Example
Input: 1 6 5 3 1 7 4 2 2 1 3 1 1 1 2 2 1 8 2 2 1 5 3 2 1 4 4 4 5 2 7 5 1 Output: 32
//7+1+8+5+4+7=32
Problem Link : http://www.spoj.com/problems/BYTESM2/
The idea : The idea here is to use dynamic programming . Use the bottom up
approach.
There is no need to use another matrix for dp as the given
matrix is enough and once we have moved from one row to another we do not need
the older values.
Now we can use the formula arr[i][j]=max(arr[i-1][j-1],arr[i-1][j],
arr[i+1][j+1])+arr[i][j].
Problem Code:
#include <iostream> #include<bits/stdc++.h> using namespace std; long long int dp[101][101]; long long int max(long long int a,long long int b,long long int c) { long long int answer=INT_MIN; answer=max(answer,a); answer=max(answer,b); answer=max(answer,c); return answer; } int main() { int T; scanf("%d",&T); while(T--) { int h,w; scanf("%d %d",&h,&w); long long int **arr; arr=new long long int*[h]; for(int i=0;i<h;i++) { arr[i]=new long long int[w]; } for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { scanf("%lld",&arr[i][j]); } } for(int i=1;i<h;i++) { for(int j=0;j<w;j++) { long long int answer=INT_MIN; long long int fromtop=INT_MIN; long long int fromdiagleft=INT_MIN; long long int fromdiagright=INT_MIN; if(j==0) { answer=max(arr[i-1][j],arr[i-1][j+1]); } else if(j==w-1) { answer=max(arr[i-1][j],arr[i-1][j-1]); } else { answer=max(arr[i-1][j+1],arr[i-1][j],arr[i-1][j-1]); } arr[i][j]=answer+arr[i][j]; } } long long int answer=INT_MIN; for(int j=0;j<w;j++) { answer=max(answer,arr[h-1][j]); } cout<<answer<<endl; } return 0; }
No comments:
Post a Comment