SPOJ - Philosophers Stone solution using DP
Solution:-
using namespace std;
#define MAX 200
int arr[MAX][MAX];
int max_val(int a,int b,int c)
{
int p=INT_MIN;
p=p>a?p:a;
p=p>b?p:b;
p=p>c?p:c;
return p;
}
int main()
{
int tc;
int a,b;
cin>>tc;
while(tc--)
{
int h,w;
cin>>h>>w;
int max_stones=0;
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
{
cin>>arr[i][j];
if(i>0)
{
a=b=INT_MIN;
if(j>0)
a=arr[i-1][j-1];
if(j<w-1)
b=arr[i-1][j+1];
arr[i][j]+=max_val(arr[i-1][j],a,b);
}
max_stones=max_stones>arr[i][j]?max_stones:arr[i][j];
}
cout<<max_stones<<endl;
}
return 0;
}
No comments:
Post a Comment