• + 0 comments
    <?php
    
    function findStartingPoint($matrix) {
        foreach ($matrix as $i => $row) {
            $col = strpos($row, 'M');
            if ($col !== false) {
                return [$i, $col];
            }
        }
        return [-1, -1];
    }
    
    function isWithinBounds($x, $y, $n, $m) {
        return $x >= 0 && $x < $n && $y >= 0 && $y < $m;
    }
    
    function countLuck($matrix, $k) {
        list($n, $m) = [count($matrix), strlen($matrix[0])];
        list($startX, $startY) = findStartingPoint($matrix);
        $dx = [-1, 1, 0, 0];
        $dy = [0, 0, -1, 1];
        $queue = [[$startX, $startY, 0]];
        $visited = array_fill(0, $n, array_fill(0, $m, false));
        $visited[$startX][$startY] = true;
    
        while (!empty($queue)) {
            list($x, $y, $waveCount) = array_shift($queue);
    
            if ($matrix[$x][$y] === '*') {
                return $waveCount === $k ? "Impressed" : "Oops!";
            }
    
            $possibleMoves = 0;
            foreach (range(0, 3) as $i) {
                $nx = $x + $dx[$i];
                $ny = $y + $dy[$i];
                if (isWithinBounds($nx, $ny, $n, $m) && !$visited[$nx][$ny] && $matrix[$nx][$ny] !== 'X') {
                    $possibleMoves++;
                }
            }
    
            foreach (range(0, 3) as $i) {
                $nx = $x + $dx[$i];
                $ny = $y + $dy[$i];
                if (isWithinBounds($nx, $ny, $n, $m) && !$visited[$nx][$ny] && $matrix[$nx][$ny] !== 'X') {
                    $visited[$nx][$ny] = true;
                    $queue[] = [$nx, $ny, $waveCount + ($possibleMoves > 1 ? 1 : 0)];
                }
            }
        }
    
        return "Oops!";
    }
    
    // Input reading
    $handle = fopen("php://stdin", "r");
    $testCases = intval(fgets($handle));
    for ($t = 0; $t < $testCases; $t++) {
        list($n, $m) = array_map('intval', explode(' ', fgets($handle)));
        $matrix = [];
        for ($i = 0; $i < $n; $i++) {
            $matrix[] = trim(fgets($handle));
        }
        $k = intval(fgets($handle));
        echo countLuck($matrix, $k) . "\n";
    }
    fclose($handle);
    
    ?>
    

    Explanation:

    1. Find Starting Point:

      • The function findStartingPoint scans the matrix to find the position of 'M' (start).
    2. Within Bounds Check:

      • The function isWithinBounds ensures the coordinates are within the grid.
    3. Breadth-First Search (BFS):

      • We use BFS to explore all possible paths.
      • Maintain a queue for BFS, starting from the 'M' position.
      • Keep track of visited positions to avoid revisiting them.
      • Count the number of decisions (wave counts) Hermione has to make using possibleMoves.
    4. Decision Points:

      • At each cell, determine the number of valid moves (cells that can be visited next).
      • If there is more than one valid move, increase the wave count.
    5. Result:

      • If the final wave count matches k, print "Impressed"; otherwise, print "Oops!".

    This code reads input directly from stdin, which is typical for competitive programming environments like HackerRank. Make sure to test this solution within the constraints of the problem to ensure it works correctly.