You are currently viewing Crack the ‘Lucky Pick’ Game Code Challenge: Win 30,000!

Crack the ‘Lucky Pick’ Game Code Challenge: Win 30,000!

is it True:”Events by Tech GIG”?

This Challenge Ends Date Ends On31 Dec 2030 11:59 PM (GMT+5.5)

Welcome to the “Lucky Pick” Game Code Challenge by Tech Gig! In this exciting arcade scenario, help your friend maximize their minimum winnings by choosing the right block on the grid. Win the top prize of 30,000 by providing an efficient and accurate solution. Participate in the “Lucky Pick” coding contest and showcase your skills in the keyprace challenge. Good luck and happy coding!

QUESTION TITLE : Game Center

You and your friend go to a game arcade where you choose to play the game Lucky Pick. In the game, there is a square grid and on each block, some money is placed on it. When a player chooses a block, the machine randomly chooses a block from the neighboring ones and the chosen block (consider 8 neighborhood). The player is awarded the money that is placed on the block that the machine selects. Your friend needs help choosing the block.

Your job is to return the block position(s) that will maximize the minimum amount your friend will win for sure. If there are more than one such block positions then the output must return for all these positions.

INPUT FORMAT

You will be given the Grid Description as –

The first line consists of the size of the square grid (N)

The next N lines each containing N numbers separated by ‘#’, each number representing the amount of money put on that block

Constraints

1 < N < 500

Output Format

You need to print the array of string containing the position(s) of a block choosing which will give the maximum amount of money which your friend will definitely win.

Sample TestCase 1 :

INPUT:

3
12#45#33
94#54#23
98#59#27

OUTPUT:

3#1

Explanation:

In the above example, if he selects the block (3,1), then under the best case, he could win is 98 and under the worst case the maximum he could win is 54. In such scenario, the worst case of block (3,1) gives your friend more money than the worst case of other blocks.

FULL QUESTION YOU GET HEAR

ANSWER: PHP

<?php
/* Read input from STDIN. Print your output to STDOUT */
$fp = fopen("php://stdin", "r");

function get_neighbors($grid, $row, $col) {
    $neighbors = [];
    for ($i = -1; $i <= 1; $i++) {
        for ($j = -1; $j <= 1; $j++) {
            if ($i == 0 && $j == 0) {
                continue;
            }
            $new_row = $row + $i;
            $new_col = $col + $j;
            if ($new_row >= 0 && $new_row < count($grid) && $new_col >= 0 && $new_col < count($grid[0])) {
                $neighbors[] = [$new_row, $new_col];
            }
        }
    }
    return $neighbors;
}

function find_maximum_minimum_winning_blocks($grid) {
    $max_min_amount = PHP_INT_MIN;
    $max_min_positions = [];

    for ($row = 0; $row < count($grid); $row++) {
        for ($col = 0; $col < count($grid[0]); $col++) {
            $min_amount = $grid[$row][$col];
            $neighbors = get_neighbors($grid, $row, $col);
            foreach ($neighbors as $neighbor) {
                $min_amount = min($min_amount, $grid[$neighbor[0]][$neighbor[1]]);
            }
            if ($min_amount > $max_min_amount) {
                $max_min_amount = $min_amount;
                $max_min_positions = [[$row + 1, $col + 1]];
            } elseif ($min_amount === $max_min_amount) {
                $max_min_positions[] = [$row + 1, $col + 1];
            }
        }
    }

    return $max_min_positions;
}

// Input
$grid_size = intval(trim(fgets($fp)));
$grid = [];
for ($i = 0; $i < $grid_size; $i++) {
    $row = explode('#', trim(fgets($fp)));
    $grid[] = array_map('intval', $row);
}

// Output
$result = find_maximum_minimum_winning_blocks($grid);
foreach ($result as $position) {
    echo $position[0] . '#' . $position[1] . PHP_EOL;
}
?>

CODE EXPLENATION:

Function get_neighbors($grid, $row, $col)

  • The code begins by defining a function called get_neighbors($grid, $row, $col).
  • This function takes a 2D grid (representing the game board) and the row and column index of a specific block on the grid.
  • It calculates and returns the positions of the neighboring blocks of the given block.

Function find_maximum_minimum_winning_blocks($grid)

  • Next, we define the main function called find_maximum_minimum_winning_blocks($grid).
  • This function takes the 2D grid as input and aims to find the blocks that offer the highest minimum amount that a player can win in the game.
  • In the find_maximum_minimum_winning_blocks function, we set variables $max_min_amount and $max_min_positions. These variables keep track of the highest minimum amount found so far and the positions of the blocks with that maximum minimum amount.

Loop through the grid

  • We then iterate through each block in the grid using nested loops (using the variables $row and $col).
  • For each block, we determine the minimum amount of money placed on its neighboring blocks (using the get_neighbors function).

Update $max_min_amount and $max_min_positions

  • If the minimum amount found for the current block is greater than the $max_min_amount, we update both $max_min_amount and $max_min_positions to reflect this new highest minimum amount.
  • If the minimum amount found is equal to the current $max_min_amount, we add the current block’s position to the list of positions with the maximum minimum amount.

Final Result

  • After looping through all the blocks, we identify the highest minimum amount ($max_min_amount) and the positions of the blocks with that maximum amount ($max_min_positions).
  • The find_maximum_minimum_winning_blocks function returns the final result – an array containing the positions of the blocks that yield the highest minimum amount to the player.

Main Part of the Code

  • In the main part of the code, we read the input from STDIN, which includes the grid size and the money amounts on each block.
  • We then call the find_maximum_minimum_winning_blocks function with the given grid.

Print the Output

  • Finally, we print the output, which will be the positions of the blocks with the highest minimum amount, sorted in row-wise order.

In summary, the code reads the input grid, calculates the minimum winning amount for each block by considering its neighboring blocks, and then finds the blocks that give the maximum minimum amount to the player. It then outputs the positions of these blocks in row-wise order. Participate in the “Lucky Pick” coding contest to showcase your skills and compete for a chance to win 30,000. Join the exciting keyprace challenge now!

TEST LINK

Leave a Reply