Đường đi có tổng lớn nhất

View as PDF

Submit solution


Points: 0.05 (partial)
Time limit: 1.0s
Memory limit: 512M
Input: stdin
Output: stdout

Problem type
Allowed languages
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch

Cho một bảng ~A~ kích thước ~m \times n~ ~(1 \le m~, ~n \le 100)~, trên đó ghi các số nguyên ~a_{ij}~ ~(|a_{ij}| \le 100)~. Một người xuất phát tại ô nào đó của cột ~1~, cần sang cột ~n~ (tại ô nào cũng được).

Quy tắc đi: Từ ô ~(i, j)~ chỉ được quyền sang một trong ~3~ ô ~(i, j + 1)~; ~(i - 1, j + 1)~; ~(i + 1, j + 1)~

Input

Dòng ~1~: Ghi hai số ~m~, ~n~ là số hàng và số cột của bảng.

~M~ dòng tiếp theo, dòng thứ ~i~ ghi đủ ~n~ số trên hàng ~i~ của bảng theo đúng thứ tự từ trái qua phải

Output

Gồm 1 dòng duy nhất ghi tổng lớn nhất tìm được

Sample Input

5 7
9 -2 6 2 1 3 4
0 -1 6 7 1 3 3
8 -2 8 2 5 3 2
1 -1 6 2 1 6 1
7 -2 6 2 1 3 7

Sample Output

41

Comments

Please read the guidelines before commenting.



  • -1
    tranductrongvip12411  commented on Feb. 1, 2026, 11:00 a.m.

    code nay:

    include <bits/stdc++.h>

    using namespace std; long long a[105][105],dp[105][105]; int main() { iosbase::syncwithstdio(false); cin.tie(NULL); long long n,m; cin>>m>>n; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++)cin>>a[i][j]; for(int i=1;i<=m;i++)dp[i][1]=a[i][1]; for(int j=2;j<=n;j++) { for(int i=1;i<=m;i++) { if(i==1)dp[i][j]=a[i][j]+max(dp[i][j-1],dp[i+1][j-1]); else if(i==m)dp[i][j]=a[i][j]+max(dp[i-1][j-1],dp[i][j-1]); else dp[i][j]=a[i][j]+max(dp[i-1][j-1],max(dp[i][j-1],dp[i+1][j-1])); } } long long kq=LLONGMIN; for(int i=1;i<=m;i++)kq=max(kq,dp[i][n]); cout<<kq; return 0; } full 100%


  • 0
    nguyentanlocefootball  commented on Jan. 8, 2026, 11:10 a.m.

    include <bits/stdc++.h>

    using namespace std; int a[105][105],dp[105][105]; int main(){ //freopen("inp","r",stdin); //freopen("out","w",stdout); iosbase::syncwithstdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; for (int i=0;i<n;++i){ for (int j=0;j<m;++j) cin>>a[i][j]; } for (int i=0;i<n;++i) dp[i][0]=a[i][0]; for (int j=1;j<m;++j){ for (int i=0;i<n;++i){ if (n==1) dp[i][j]=dp[i][j-1]+a[i][j]; else if (i==0) dp[i][j]=max(dp[i][j-1],dp[i+1][j-1])+a[i][j]; else if (i==n-1) dp[i][j]=max(dp[i][j-1],dp[i-1][j-1])+a[i][j]; else dp[i][j]=max({dp[i-1][j-1],dp[i][j-1],dp[i+1][j-1]})+a[i][j]; } } int ans=INTMIN; for (int i=0;i<n;++i) ans=max(ans,dp[i][m-1]); cout<<ans;
    }


  • 1
    hanguyen140210  commented on Jan. 6, 2026, 7:15 a.m.

    include<bits/stdc++.h>

    using namespace std;

    define vec vector

    define ll long long

    ll i,j; int main(){ iosbase::syncwithstdio(0); cin.tie(0);cout.tie(0); ll n,m;cin>>n>>m; vec<vec>a(n+1,vec<ll>(m+1)),dp(n+1,vec<ll>(m+1,-10001)); for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ cin>>a[i][j]; } } for(i=1;i<=n;i++)dp[i][1]=a[i][1]; for(j=2;j<=m;j++){ for(i=1;i<=n;i++){ if(i==n){ dp[i][j]=max(dp[i][j-1],dp[i-1][j-1])+a[i][j]; } else dp[i][j]=max(dp[i-1][j-1],max(dp[i][j-1],dp[i+1][j-1]))+a[i][j]; } } ll ma=INT</vec>MIN; for(i=1;i<=n;i++)ma=max(ma,dp[i][m]); cout<<ma; }


  • -15
    THPTHD_Hieu  commented on May 15, 2025, 6:48 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • -7
      RussVN123  commented on May 21, 2025, 5:39 a.m.

      This comment is hidden due to too much negative feedback. Show it anyway.


    • -7
      anle82917  commented on May 21, 2025, 5:21 a.m.

      This comment is hidden due to too much negative feedback. Show it anyway.


  • -6
    daotrungkiena1k46  commented on April 20, 2025, 1:19 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -7
    memme  commented on Jan. 12, 2025, 3:07 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -4
    HUNG2010  commented on Dec. 14, 2024, 3:36 a.m.

    include<bits/stdc++.h>

    define maxN 105

    using namespace std; const int INF = 1e9;

    int n, m; int a[maxN][maxN], f[maxN][maxN];

    void enter() { cin >> m >> n; for (int i =1; i <= m; i++){ for (int j =1; j <= n; j++) cin >> a[i][j]; } }

    void solve(){ for (int i = 0; i <= m; i++) f[i][0] = 0; for (int i =0; i <= n; i++) f[0][i] = -INF; for (int i =0; i <= n; i++) f[m+1][i] = -INF; for (int j = 1; j <=n; j++){ for (int i =1; i <= m; i++) f[i][j] = max(max(f[i-1][j-1], f[i][j-1]), f[i+1][j-1]) + a[i][j];
    } int res = -INF; for (int i = 1; i <=m; i++) res = max(res, f[i][n]); cout<<res; }

    int main(){ enter(); solve(); }


  • 1
    nguyenduychan2024  commented on Oct. 23, 2024, 11:01 a.m. edited

    =3


  • -11
    m1nkpk4nn  commented on Aug. 6, 2024, 4:02 p.m. edited

    This comment is hidden due to too much negative feedback. Show it anyway.


  • 1
    melondepchai  commented on June 13, 2024, 8:47 a.m.

    4 test cuối xử lý như nào vậy mọi người, em sai 4 test cuối mà không biết fix sao?!!


  • -4
    HUY312132  commented on Jan. 19, 2024, 7:27 a.m. edited

    s


  • -3
    Tloi_07  commented on Jan. 5, 2024, 1:29 a.m.

    tloi


  • -8
    Quangdpm  commented on Dec. 23, 2023, 3:06 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -67
    themluachon2008  commented on Oct. 8, 2023, 9:20 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -4
    Hiiragi_Sergan  commented on Sept. 1, 2023, 9:15 a.m.

    Fix bug tùm lum luôn x__x


  • -44
    ksomg  commented on Jan. 4, 2022, 4:10 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -62
    DoanTungLamKoCopyCode  commented on Oct. 31, 2021, 1:57 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • 0
    mikamikaela  commented on Aug. 3, 2021, 7:40 a.m.

    Tác giả cho em hỏi có thực sự là (-100 <= A[i][j] <= 100) với mọi i, j không tác giả ơi.

    Vì em để MIN là -(1e9+7) thì AC, còn để MIN là -107 thì tạch :V


    • 18
      ngfam  commented on Aug. 3, 2021, 3:10 p.m.

      ~ abs(A[i][j]) \leq 100 ~ thì tổng đường đi có thể xuống ~-100 \times M~ nhé.


      • -13
        mikamikaela  commented on Aug. 5, 2021, 3:32 p.m.

        This comment is hidden due to too much negative feedback. Show it anyway.


        • -6
          jalsol  commented on Aug. 5, 2021, 11:45 p.m.

          This comment is hidden due to too much negative feedback. Show it anyway.


  • -214
    dungp24  commented on May 30, 2021, 4:11 a.m. edited

    This comment is hidden due to too much negative feedback. Show it anyway.