Finding destination using 2D matrix
package example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TruckProblem {
public static void main(String[] args) {
// input 1
List<List<Integer>> area = new ArrayList<>();
area.add(Arrays.asList(1, 0, 0));
area.add(Arrays.asList(1, 0, 0));
area.add(Arrays.asList(1, 9, 1));
Integer[][] areaArray = convertListToArray(area);
// List<List<Integer>> pathArray = getRegionSize(areaArray);
int size = getBiggestRegion(areaArray);
System.out.println("path array:" + size);
// input 2
area = new ArrayList<>();
area.add(Arrays.asList(1, 0, 1));
area.add(Arrays.asList(1, 0, 1));
area.add(Arrays.asList(1, 9, 1));
areaArray = convertListToArray(area);
// List<List<Integer>> pathArray = getRegionSize(areaArray);
size = getBiggestRegion(areaArray);
System.out.println("path array:" + size);
}
static Integer[][] convertListToArray(List<List<Integer>> area) {
Integer[][] areaArray = new Integer[area.size()][];
for (int i = 0; i < area.size(); i++) {
List<Integer> list = area.get(i);
System.out.println(list);
areaArray[i] = list.toArray(new Integer[list.size()]);
}
return areaArray;
}
public static int getRegionSize(Integer[][] matrix, int row, int column) {
if (row < 0 || column < 0 || row >= matrix.length || column >= matrix[row].length) {
return 0;
}
if (matrix[row][column] == 0 || matrix[row][column] == 9) {
return 0;
}
//System.out.println(row + " " + column);
matrix[row][column] = 0;
int size = 1;
for (int r = row - 1; r <= row + 1; r++) {
for (int c = column - 1; c <= column + 1; c++) {
if (r != row || c != column) {
// System.out.println(r +" "+ c);
size += getRegionSize(matrix, r, c);
}
}
//System.out.println("*** ");
}
return size;
}
public static int getBiggestRegion(Integer[][] matrix) {
int maxRegion = 0;
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
if (matrix[row][column] == 1) {
int size = getRegionSize(matrix, row, column);
maxRegion = Math.max(size, maxRegion);
}
if (matrix[row][column] == 9) {
break;
}
}
}
return maxRegion;
}
}
package example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TruckProblem {
public static void main(String[] args) {
// input 1
List<List<Integer>> area = new ArrayList<>();
area.add(Arrays.asList(1, 0, 0));
area.add(Arrays.asList(1, 0, 0));
area.add(Arrays.asList(1, 9, 1));
Integer[][] areaArray = convertListToArray(area);
// List<List<Integer>> pathArray = getRegionSize(areaArray);
int size = getBiggestRegion(areaArray);
System.out.println("path array:" + size);
// input 2
area = new ArrayList<>();
area.add(Arrays.asList(1, 0, 1));
area.add(Arrays.asList(1, 0, 1));
area.add(Arrays.asList(1, 9, 1));
areaArray = convertListToArray(area);
// List<List<Integer>> pathArray = getRegionSize(areaArray);
size = getBiggestRegion(areaArray);
System.out.println("path array:" + size);
}
static Integer[][] convertListToArray(List<List<Integer>> area) {
Integer[][] areaArray = new Integer[area.size()][];
for (int i = 0; i < area.size(); i++) {
List<Integer> list = area.get(i);
System.out.println(list);
areaArray[i] = list.toArray(new Integer[list.size()]);
}
return areaArray;
}
public static int getRegionSize(Integer[][] matrix, int row, int column) {
if (row < 0 || column < 0 || row >= matrix.length || column >= matrix[row].length) {
return 0;
}
if (matrix[row][column] == 0 || matrix[row][column] == 9) {
return 0;
}
//System.out.println(row + " " + column);
matrix[row][column] = 0;
int size = 1;
for (int r = row - 1; r <= row + 1; r++) {
for (int c = column - 1; c <= column + 1; c++) {
if (r != row || c != column) {
// System.out.println(r +" "+ c);
size += getRegionSize(matrix, r, c);
}
}
//System.out.println("*** ");
}
return size;
}
public static int getBiggestRegion(Integer[][] matrix) {
int maxRegion = 0;
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
if (matrix[row][column] == 1) {
int size = getRegionSize(matrix, row, column);
maxRegion = Math.max(size, maxRegion);
}
if (matrix[row][column] == 9) {
break;
}
}
}
return maxRegion;
}
}
Output:
[1, 0, 0]
[1, 0, 0]
[1, 9, 1]
path array:3
[1, 0, 1]
[1, 0, 1]
[1, 9, 1]
path array:3