Philosophers Stone
Given below code is for BYTESM2 spoj or Philosophers Stone spoj
Hint:- Simple DP
#include <iostream> #include <cstdlib> #include <algorithm> using namespace std; int main() { int t; cin>>t; while(t--) { int h , m; cin>>h>>m; int arr[h+9][m+9]; for(int i = 0 ; i < h ; i++) for(int j = 0 ; j < m ; j++) cin>>arr[i][j]; int ma = -1; for(int i = 0 ; i < m ; i++) ma = max(ma , arr[0][i]); for(int i = 1 ; i < h ; i++) { ma = -1; for(int j = 0 ; j < m ; j++) { if(j>0 && j<m-1) arr[i][j] = max(arr[i-1][j] + arr[i][j] , max(arr[i-1][j-1]+arr[i][j] , arr[i-1][j+1]+arr[i][j])); else if(j>0) arr[i][j] = max(arr[i-1][j]+arr[i][j] ,arr[i-1][j-1]+arr[i][j]); else if(j<m-1) arr[i][j] = max(arr[i-1][j]+arr[i][j],arr[i-1][j+1]+arr[i][j]); ma = max(arr[i][j] , ma); } } cout<<ma<<endl; } return 0; }
thanks for the code...
ReplyDelete#include
ReplyDeleteusing namespace std;
int findmax(int a,int b,int c)
{
int max=a;
if(max>d[i][j];
for(j=1;j<=w;j++)
dp[1][j]=d[1][j];
//storing inf value
for(i=1;i<h;i++)
dp[i][0]=-1;
//recurrence relation
for(i=2;i<=h;i++)
for(j=1;j<=w;j++)
dp[i][j]=d[i][j]+findmax(dp[i-1][j],dp[i-1][j+1],dp[i-1][j-1]);
//answer
int ans=dp[h][1];
for(j=2;j<=w;j++)
if(ans<dp[h][j])
ans=dp[h][j];
printf("%d\n",ans);
}
}
Why this answer is wrong
#include
ReplyDeleteusing namespace std;
int findmax(int a,int b,int c)
{
int max=a;
if(max>d[i][j];
for(j=1;j<=w;j++)
dp[1][j]=d[1][j];
//storing inf value
for(i=1;i<h;i++)
dp[i][0]=-1;
//recurrence relation
for(i=2;i<=h;i++)
for(j=1;j<=w;j++)
dp[i][j]=d[i][j]+findmax(dp[i-1][j],dp[i-1][j+1],dp[i-1][j-1]);
//answer
int ans=dp[h][1];
for(j=2;j<=w;j++)
if(ans<dp[h][j])
ans=dp[h][j];
printf("%d\n",ans);
}
}