Friday 21 August 2015

SPOJ-MISERMAN


hello friends this a new post on dp(dynamic programming)

its a rather a easy program... for novice look through the code and comment


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<bits/stdc++.h>
int min(int x,int y)
{
    return (x<y)?x:y;
}
int main()
{
    int n,m,a[105][105]={0};
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=m+1;j++)
        {
            if(!j || j==m+1)
                a[i][j]=1000;
            else
                scanf("%d",&a[i][j]);
        }
    }
    for(int i=n-2;i>=0;i--)
    {
        for(int j=1;j<=m;j++)
            a[i][j]+=min(min(a[i+1][j-1],a[i+1][j]),a[i+1][j+1]);
    }
    int x=1000;
    for(int j=1;j<=m;j++)
        x=min(x,a[0][j]);
    printf("%d\n",x);
    return 0;
}

No comments:

Post a Comment