Advertisement
nguyenvanquan7826

QBMAX

Jan 13th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int max_abc(int a,int b, int c)
  8. {
  9.     return max(a,max(b,c));
  10. };
  11. int main()
  12. {
  13.     int n,m,i,j,max;
  14.     cin>>n>>m;
  15.     int a[n+2][m+2], b[n+2][m+2];
  16.    
  17.     for (j=0; j<=m+1;j++)
  18.         b[0][j] = b[n+1][j] = 0;
  19.     for (i=0; i<=n+1; i++)
  20.         b[i][0] = b[i][m+1] = 0;
  21.    
  22.     for (i=1; i<=n; i++)
  23.         for (j=1; j<=m; j++)
  24.             cin>>a[i][j];
  25.            
  26.     max = a[1][1];
  27.     for (i=1; i<=n; i++)   
  28.     {
  29.         if (max < a[i][1]) max = a[i][1];
  30.         b[i][1] = a[i][1];
  31.     }
  32.    
  33.     for (j=1; j<=m; j++)
  34.     {
  35.         for (i=1; i<=n; i++)       
  36.             b[i][j] = a[i][j] + max_abc(b[i-1][j-1], b[i][j-1], b[i+1][j-1]);
  37.         max = b[1][j];
  38.         for (i=2; i<=n; i++)   
  39.             if (max < b[i][j]) max = b[i][j];
  40.     }
  41.    
  42.     cout<<max;
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement