fork(1) download
  1. java
  2. import java.util.List;
  3.  
  4. class Result {
  5. /*
  6.   * Complete the 'search_with_energy' function below.
  7.   *
  8.   * The function is expected to return an INTEGER.
  9.   * The function accepts following parameters:
  10.   * 1. CHARACTER ARRAY charArray
  11.   * 2. CHARACTER 2D ARRAY matrix
  12.   * 3. INTEGER energy
  13.   */
  14.  
  15. public static int search_with_energy(List<Character> charArray, List<List<Character>> matrix, int energy) {
  16. int rows = matrix.size();
  17. int cols = matrix.get(0).size();
  18.  
  19. // Helper function for depth-first search
  20. boolean dfs(int row, int col, int currentEnergy, int charIndex) {
  21. // Base cases
  22. if (charIndex == charArray.size()) {
  23. return true; // Sequence found
  24. }
  25. if (currentEnergy <= 0) {
  26. return false; // Energy exhausted
  27. }
  28.  
  29. char targetChar = charArray.get(charIndex);
  30.  
  31. // Explore adjacent cells
  32. int[] dr = {0, 0, 1, -1}; // Row offsets
  33. int[] dc = {1, -1, 0, 0}; // Column offsets
  34.  
  35. for (int i = 0; i < 4; i++) {
  36. int newRow = row + dr[i];
  37. int newCol = col + dc[i];
  38.  
  39. // Check boundaries and character match
  40. if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols &&
  41. (matrix.get(newRow).get(newCol) == targetChar || matrix.get(newRow).get(newCol) == '*')) {
  42. if (dfs(newRow, newCol, currentEnergy - 1, charIndex + 1)) {
  43. return true; // Sequence found
  44. }
  45. }
  46. }
  47. return false; // No path found from this cell
  48. }
  49.  
  50. // Start DFS from each cell in the matrix
  51. for (int i = 0; i < rows; i++) {
  52. for (int j = 0; j < cols; j++) {
  53. if (matrix.get(i).get(j) == charArray.get(0) || matrix.get(i).get(j) == '*') {
  54. if (dfs(i, j, energy, 0)) {
  55. return 1; // Sequence found
  56. }
  57. }
  58. }
  59. }
  60. return 0; // Sequence not found
  61. }
  62. }
  63.  
Success #stdin #stdout 0.04s 25976KB
stdin
Standard input is empty
stdout
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
    }
}