|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.Queue; |
| 5 | + |
| 6 | +/** |
| 7 | + * 1219. Path with Maximum Gold |
| 8 | + * |
| 9 | + * In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. |
| 10 | + * Return the maximum amount of gold you can collect under the conditions: |
| 11 | + * Every time you are located in a cell you will collect all the gold in that cell. |
| 12 | + * From your position you can walk one step to the left, right, up or down. |
| 13 | + * You can't visit the same cell more than once. |
| 14 | + * Never visit a cell with 0 gold. |
| 15 | + * You can start and stop collecting gold from any position in the grid that has some gold. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * Input: grid = [[0,6,0],[5,8,7],[0,9,0]] |
| 19 | + * Output: 24 |
| 20 | + * Explanation: |
| 21 | + * [[0,6,0], |
| 22 | + * [5,8,7], |
| 23 | + * [0,9,0]] |
| 24 | + * Path to get the maximum gold, 9 -> 8 -> 7. |
| 25 | + * |
| 26 | + * Example 2: |
| 27 | + * Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] |
| 28 | + * Output: 28 |
| 29 | + * Explanation: |
| 30 | + * [[1,0,7], |
| 31 | + * [2,0,6], |
| 32 | + * [3,4,5], |
| 33 | + * [0,3,0], |
| 34 | + * [9,0,20]] |
| 35 | + * Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7. |
| 36 | + * |
| 37 | + * Constraints: |
| 38 | + * 1 <= grid.length, grid[i].length <= 15 |
| 39 | + * 0 <= grid[i][j] <= 100 |
| 40 | + * There are at most 25 cells containing gold. |
| 41 | + * */ |
| 42 | +public class _1219 { |
| 43 | + public static class Solution1 { |
| 44 | + public int getMaximumGold(int[][] grid) { |
| 45 | + Queue<int[]> queue = new LinkedList<>(); |
| 46 | + int m = grid.length; |
| 47 | + int n = grid[0].length; |
| 48 | + for (int i = 0; i < m; i++) { |
| 49 | + for (int j = 0; j < n; j++) { |
| 50 | + if (grid[i][j] > 0) { |
| 51 | + queue.offer(new int[]{i, j}); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + int maxGold = 0; |
| 56 | + while (!queue.isEmpty()) { |
| 57 | + int[] start = queue.poll(); |
| 58 | + boolean[][] visited = new boolean[m][n]; |
| 59 | + visited[start[0]][start[1]] = true; |
| 60 | + maxGold = Math.max(maxGold, backtracking(grid, start, grid[start[0]][start[1]], visited)); |
| 61 | + } |
| 62 | + return maxGold; |
| 63 | + } |
| 64 | + |
| 65 | + int[] directions = new int[]{0, 1, 0, -1, 0}; |
| 66 | + private int backtracking(int[][] grid, int[] start, int gold, boolean[][] visited) { |
| 67 | + int max = gold; |
| 68 | + for (int i = 0; i < directions.length - 1; i++) { |
| 69 | + int nextX = start[0] + directions[i]; |
| 70 | + int nextY = start[1] + directions[i + 1]; |
| 71 | + if (nextX >= 0 && nextX < grid.length && nextY >= 0 && nextY < grid[0].length && !visited[nextX][nextY] && grid[nextX][nextY] > 0) { |
| 72 | + visited[nextX][nextY] = true; |
| 73 | + max = Math.max(max, backtracking(grid, new int[]{nextX, nextY}, gold + grid[nextX][nextY], visited)); |
| 74 | + visited[nextX][nextY] = false; |
| 75 | + } |
| 76 | + } |
| 77 | + return max; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments