java class Result { /* * Complete the 'search_with_energy' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. CHARACTER ARRAY charArray * 2. CHARACTER 2D ARRAY matrix * 3. INTEGER energy */ public static int search_with_energy(List<Character> charArray, List<List<Character>> matrix, int energy) { int rows = matrix.size(); int cols = matrix.get(0).size(); // Helper function for depth-first search boolean dfs(int row, int col, int currentEnergy, int charIndex) { // Base cases if (charIndex == charArray.size()) { return true; // Sequence found } if (currentEnergy <= 0) { return false; // Energy exhausted } char targetChar = charArray.get(charIndex); // Explore adjacent cells int[] dr = {0, 0, 1, -1}; // Row offsets int[] dc = {1, -1, 0, 0}; // Column offsets for (int i = 0; i < 4; i++) { int newRow = row + dr[i]; int newCol = col + dc[i]; // Check boundaries and character match if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && (matrix.get(newRow).get(newCol) == targetChar || matrix.get(newRow).get(newCol) == '*')) { if (dfs(newRow, newCol, currentEnergy - 1, charIndex + 1)) { return true; // Sequence found } } } return false; // No path found from this cell } // Start DFS from each cell in the matrix for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (matrix.get(i).get(j) == charArray.get(0) || matrix.get(i).get(j) == '*') { if (dfs(i, j, energy, 0)) { return 1; // Sequence found } } } } return 0; // Sequence not found } }
Standard input is empty
java import java.util.List; class Result { /* * Complete the 'search_with_energy' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. CHARACTER ARRAY charArray * 2. CHARACTER 2D ARRAY matrix * 3. INTEGER energy */ public static int search_with_energy(List<Character> charArray, List<List<Character>> matrix, int energy) { int rows = matrix.size(); int cols = matrix.get(0).size(); // Helper function for depth-first search boolean dfs(int row, int col, int currentEnergy, int charIndex) { // Base cases if (charIndex == charArray.size()) { return true; // Sequence found } if (currentEnergy <= 0) { return false; // Energy exhausted } char targetChar = charArray.get(charIndex); // Explore adjacent cells int[] dr = {0, 0, 1, -1}; // Row offsets int[] dc = {1, -1, 0, 0}; // Column offsets for (int i = 0; i < 4; i++) { int newRow = row + dr[i]; int newCol = col + dc[i]; // Check boundaries and character match if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && (matrix.get(newRow).get(newCol) == targetChar || matrix.get(newRow).get(newCol) == '*')) { if (dfs(newRow, newCol, currentEnergy - 1, charIndex + 1)) { return true; // Sequence found } } } return false; // No path found from this cell } // Start DFS from each cell in the matrix for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (matrix.get(i).get(j) == charArray.get(0) || matrix.get(i).get(j) == '*') { if (dfs(i, j, energy, 0)) { return 1; // Sequence found } } } } return 0; // Sequence not found } }