Click here to Skip to main content
14,971,553 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
Richard MacCutchan28-Dec-19 21:49
mveRichard MacCutchan28-Dec-19 21:49 
AnswerRe: Abstract Factory Design/Linked List Problem Pin
Stefan_Lang6-Jan-20 0:22
mveStefan_Lang6-Jan-20 0:22 
Questionqueues to find minimum time to serve all. Pin
Jiopik27-Dec-19 19:01
MemberJiopik27-Dec-19 19:01 
AnswerRe: queues to find minimum time to serve all. Pin
Richard MacCutchan27-Dec-19 22:01
mveRichard MacCutchan27-Dec-19 22:01 
AnswerRe: queues to find minimum time to serve all. Pin
leon de boer28-Dec-19 14:40
Memberleon de boer28-Dec-19 14:40 
QuestionHow to check if USB filter driver be installed by MFC? Pin
yufengchien26-Dec-19 22:19
Memberyufengchien26-Dec-19 22:19 
AnswerRe: How to check if USB filter driver be installed by MFC? Pin
Richard MacCutchan26-Dec-19 23:44
mveRichard MacCutchan26-Dec-19 23:44 
QuestionKnight move, right direction, dynamic programming to get the maximum cost path from top left to right bottom. Pin
Jiopik25-Dec-19 0:25
MemberJiopik25-Dec-19 0:25 
I am trying to solve this problem through dynamic programming:

You are given a matrix of n rows and m columns. There’s an integer number on each cell of the board and the rabbit staying at the upper-left corner.

Collect the greatest sum possible, such that the rabbit can move in only two directions:

2 cells to the right and 1 cell down (x+2, y+1); 2 cells down and 1 cell to the right (x+1, y+2);

Input:

The first line contains two naturals n and m (1 ≤ n, m ≤ 10^3) – the quantity of rows and columns of the matrix.

The next n lines contain m numbers – the values of the matrix elements.

The upper-left corner’s coordinates are (1, 1), the lower-right corner’s – (n, m).

Output:

The greatest sum possibly collected. If the r rabbit can’t reach the lower-right corner, output «-».

Input1:

3 3

5 0 0

0 1 2

1 0 1

Output1:

-

Input2:

4 4

5 2 1 0

1 0 0 0

2 1 3 0

0 0 1 7

Output2:

13

This the code I tried to develop:


#include <iostream>
//#include <cmath>
//#include <climits>
//#include <string>
#include <algorithm>

using namespace std;
void findMaxSum(int *a[], int r, int c)
{

    int **res = new int*[r];
    for (int i = 0; i < r; i++) {
        res[i] = new int[c];
        for (int j = 0; j < c; j++)
            res[i][j] = -1;
    }

    for (int i = 0; i < r-1; i++) {
        for (int j = i; j < c-1; j++) {
            res[i + 1][j + 2] = max(a[i][j] + a[i + 1][j + 2], res[i + 1][j + 2]);
            res[i + 2][j + 1] = max(a[i][j] + a[i + 2][j + 1], res[i + 2][j + 1]);
        }
    }

    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++)
            cout << res[i][j] << " ";
        cout << endl;
    }
    delete[] res;

    /*int result = res[r - 1][c - 1];
    if (result == -1)
        cout << "-";
    else
        cout << result;*/
}

int main() {

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int r, c;
    cin >> r >> c;

    int **a = new int*[r];

    for (int i = 0; i < r; i++) {
        a[i] = new int[c];
    }

    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++)
            cin >> a[i][j];
    }


    findMaxSum(a, r, c);
    delete[] a;
    return 0;
}


Could you help which right for or while loop to use to calculate such sum?
AnswerRe: Knight move, right direction, dynamic programming to get the maximum cost path from top left to right bottom. Pin
Richard MacCutchan26-Dec-19 4:25
mveRichard MacCutchan26-Dec-19 4:25 
QuestionIRichEditOle::InsertObject shifts text one byte Pin
ForNow21-Dec-19 18:18
MemberForNow21-Dec-19 18:18 
GeneralRe: IRichEditOle::InsertObject shifts text one byte Pin
Richard MacCutchan21-Dec-19 22:07
mveRichard MacCutchan21-Dec-19 22:07 
GeneralRe: IRichEditOle::InsertObject shifts text one byte Pin
ForNow22-Dec-19 5:09
MemberForNow22-Dec-19 5:09 
GeneralRe: IRichEditOle::InsertObject shifts text one byte Pin
Richard MacCutchan22-Dec-19 22:20
mveRichard MacCutchan22-Dec-19 22:20 
GeneralRe: IRichEditOle::InsertObject shifts text one byte Pin
ForNow23-Dec-19 1:04
MemberForNow23-Dec-19 1:04 
GeneralRe: IRichEditOle::InsertObject shifts text one byte Pin
Richard MacCutchan23-Dec-19 2:03
mveRichard MacCutchan23-Dec-19 2:03 
QuestionCASE SOLVED AND CLOSED cout and cerr execute out of coded sequence Pin
Vaclav_21-Dec-19 10:13
MemberVaclav_21-Dec-19 10:13 
AnswerRe: SOLVED cout and cerr execute out of coded sequence Pin
Richard MacCutchan21-Dec-19 21:32
mveRichard MacCutchan21-Dec-19 21:32 
AnswerRe: CASE SOLVED AND CLOSED cout and cerr execute out of coded sequence Pin
Richard MacCutchan22-Dec-19 22:55
mveRichard MacCutchan22-Dec-19 22:55 
QuestionSOLVED CASE CLOSED cout in color and perror - revisited Pin
Vaclav_20-Dec-19 4:46
MemberVaclav_20-Dec-19 4:46 
AnswerRe: cout in color and perror - revisited Pin
leon de boer20-Dec-19 5:01
Memberleon de boer20-Dec-19 5:01 
GeneralRe: cout in color and perror - revisited Pin
Vaclav_20-Dec-19 5:48
MemberVaclav_20-Dec-19 5:48 
AnswerRe: cout in color and perror - revisited Pin
Richard MacCutchan20-Dec-19 6:07
mveRichard MacCutchan20-Dec-19 6:07 
GeneralRe: cout in color and perror - revisited Pin
Vaclav_21-Dec-19 3:10
MemberVaclav_21-Dec-19 3:10 
GeneralRe: cout in color and perror - revisited Pin
Richard MacCutchan21-Dec-19 3:34
mveRichard MacCutchan21-Dec-19 3:34 
GeneralRe: cout in color and perror - revisited Pin
Vaclav_21-Dec-19 9:05
MemberVaclav_21-Dec-19 9:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.