Thứ Ba, 19 tháng 7, 2022

robot578 c++

#include <bits/stdc++.h>
using namespace std;

int n, m;
int a[1005][1005];
long long f[1005][1005];

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

}

void truyvet()
{
    int i = n, j = m,d=0;
    char res[10000];
    while (i>1||j>1)
    {
        if (f[i][j] == f[i-1][j] + a[i][j])
        {
            i--;
            d++;res[d]='D';
        }
        else
            if (f[i][j] == f[i][j-1] + a[i][j])
            {
                j--;
                d++;res[d]='R';
            }
    }
    for(int i=d;i>=1;i--)
        cout<<res[i];
}

void giai(){
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            f[i][j] = max(f[i-1][j], f[i][j-1]) + a[i][j];
        }
    }
    cout<<f[n][m]<<endl;
    truyvet();
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
 //   freopen("ROBOT578.inp", "r", stdin);
//    freopen("ROBOT578.out", "w", stdout);
    nhap();
    giai();
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.