简体   繁体   中英

Robot Coin Collection Problem DYNAMIC PROGRAMMING

This is a classic DP question where there is a nxm board which has coin placed on some of the squares. A robot travels from the upper left corner to the bottom right corner collecting coins. The robot can only move from left to right or from top to down at a time. You have to find the maximum number of coins picked up by the robot and also print that particular path on which robot collects that max number of coins.

I figured out how to calculate the maximum coins that it will collect but I,m stuck at the printing of the path part. Can any of you guys help me out?

Here is my code

public class robotCoin {
public static int collectCoins(int [][] board) {

    int [][] F = new int[board.length][board[0].length];

    int previousStage;
    F[0][0] = board[0][0];

    for(int j=1;j<board[0].length;j++) {//Filling the first row as robot can only 
                                        //enter from left side
        F[0][j]= F[0][j-1]+board[0][j];
    }
    System.out.println("(0,0)");
    for(int i = 1; i<board.length;i++) {
        F[i][0] = F[i-1][0]+ board[i][0]; //In the first column he can only move from top to down

        for(int j=1;j<board[0].length;j++) {
            if(F[i-1][j] > F[i][j-1]) 
                previousStage = F[i-1][j];

            else previousStage =  F[i][j-1];


            F[i][j] = previousStage + board[i][j];
        }
    }


    return F[board.length-1][board[0].length-1];

}   

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int [][]board = {
                {0,0,0,0,1,0},
                {0,1,0,1,0,0},
                {0,0,0,1,0,1},
                {0,0,1,0,0,1},
                {1,0,0,1,0,0}
                };
        System.out.println("Total="+collectCoins(board));
    }

}


I coded this problem in python and this is the particular function of the path

def path(self):

    # to accessing the values in the board in reverse mode
    row = self.row - 1  # 4
    column = self.column - 1  # 5

    # playing the first and last values in the board
    self.gameBoard[row][column] = '*'

    # finish when i=1 as first rew already assigned
    while row >= 1:
        if column != 0 and (self.gameBoard[row][column - 1] > self.gameBoard[row - 1][column]):  # leftward
            # moving to the left
            self.gameBoard[row][column - 1] = '*'
            column = column - 1
        else:
            # moving to the up
            self.gameBoard[row - 1][column] = '*'  # upward
            row = row - 1

    # the remaining cells path of the first row
    column = column - 1
    while column >= 0:
        self.gameBoard[0][column] = '*'
        column = column - 1

    return self.gameBoard

check this to see all algorithm: github repo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM