Question: 1 - public class DynamicProgrammingAlgorithm { public static int mapMinPath ( int [ ] [ ] map, int N ) { int [ ] [

1- public class DynamicProgrammingAlgorithm {
public static int mapMinPath(int[][] map, int N){
int[][] map1= new int[N][N];
// Initialize the map1 array
for (int i =0 ; i < map1.length ; i++){
for(int j =0 ; j < map1[i].length ; j++){
map1[i][j]= Integer.MAX_VALUE;
}
}
map1[0][0]= map[0][0];
// Fill the map1 table
for (int i =0; i < N; i++){
for (int j =0; j < N; j++){
if (i >0){
map1[i][j]= Math.min(map1[i][j], map1[i -1][j]+ map[i][j]); //up
}
if (j >0){
map1[i][j]= Math.min(map1[i][j], map1[i][j -1]+ map[i][j]); // left
}
if (i < N -1){
map1[i +1][j]= Math.min(map1[i +1][j], map1[i][j]+ map[i +1][j]); //down
}
if (j < N -1){
map1[i][j +1]= Math.min(map1[i][j +1], map1[i][j]+ map[i][j +1]); //right
}
}
}
return map1[N -1][N -1];
}2-class BruteForce {
static int N;
static int[][] map;
static boolean[][] visited;
static int minCost = Integer.MAX_VALUE;
static void bruteForce(int row, int column, int cost){
cost += map[row][column];
if (row == N -1 && column == N -1){
minCost = Math.min(minCost, cost);
return;
}
visited[row][column]= true;
int[] dRow ={1,0}; // down
int[] dColumn ={0,1}; // right
for (int i =0; i <2; i++){
int newRow = row + dRow[i], newColumn = column + dColumn[i];
if (newRow < N && newColumn < N && !visited[newRow][newColumn]){
bruteForce(newRow, newColumn, cost);
}
}
visited[row][column]= false;
} write Time complexity

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!