Menu
Question Index
...


Write a program to find the best way to collect maximum amount from the money grid. The start position and the end position are given. For every step taken, an amount of 15 will be deducted. There are bombs placed in the money grid and they are marked as -99. The way should not step on the bombs.

Input (int[][], Position, Position) Output (String)
{{-10, -99, -30}, {10, 30, -10}, {50, 0, 80}}, [1, 0], [2, 2] BestWayDetails [positions=[(1,0), (2,0), (2,1), (2,2)], amount=95]
{{5, 0, 13}, {35, -99, 50}}, [1, 0], [1, 2] BestWayDetails [positions=[(1,0), (0,0), (0,1), (0,2), (1,2)], amount=43]
{{0, 23, 33, 19, 11}, {-1, -99, 99, -1, 45}, {4, -13, 23, 0, 13}, {6, 12, -99, 1, 7}, {19, 17, 18, 3, 0}, {3, -11, 50, 2, 1}}, [0, 2], [5, 2] BestWayDetails [positions=[(0,2), (0,3), (0,4), (1,4), (1,3), (1,2), (2,2), (2,1), (3,1), (4,1), (4,2), (5,2)], amount=148]
{{25, 1, 0, 15}, {-1, 3, -99, 0}, {12, -1, -11, 2}}, [0, 0], [0, 3] BestWayDetails [positions=[(0,0), (0,1), (0,2), (0,3)], amount=-4]
{{-99, 2, 3, 4, -99, 8}, {0, 1, -99, 5, 6, 7}}, [1, 0], [0, 5] BestWayDetails [positions=[(1,0), (1,1), (0,1), (0,2), (0,3), (1,3), (1,4), (1,5), (0,5)], amount=-84]
{{-20, 16}, {21, -15}, {-22, 20}, {23, 10}, {24, 26}, {-25, 30}}, [0, 0], [5, 1] BestWayDetails [positions=[(0,0), (1,0), (1,1), (2,1), (3,1), (3,0), (4,0), (4,1), (5,1)], amount=-1]

class CollectMaximumAmount

{    public static void main(String s[])
    {
        int[][] input = {{0, 80, 0}, {25, 50, 63}, {-99, -20, 10}};
        System.out.println(getBestWay(input, new Position(0, 0), new Position(2, 2)));

    }


public static BestWayDetails getBestWay(int[][] input, Position source, Position destination) {
//Write code here to get the best way to collect maximum amount
}

//If required, write any additional methods here
}
class Position {

int rowPosition;
int columnPosition;

public Position(int rowPosition, int columnPosition) {
this.rowPosition = rowPosition;
this.columnPosition = columnPosition;
}

@Override
public boolean equals(Object obj) {
return rowPosition == ((Position) obj).rowPosition && columnPosition == ((Position) obj).columnPosition;
}

@Override
public String toString() {
return "(" + rowPosition + "," + columnPosition + ")";
}
}

class BestWayDetails {

List<Position> positions = new ArrayList<Position>();
int amount;

@Override
public String toString() {
return "BestWayDetails [positions=" + positions + ", amount=" + amount + "]";
}
}

Doubts

Problems

Topic: Collection Framework In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App