Created
July 15, 2024 05:26
-
-
Save lechuhuuha/ab56eb83796145a17824886f3784256f to your computer and use it in GitHub Desktop.
test 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function createField($H, $W) | |
{ | |
$field = array(); | |
for ($i = 0; $i < $H; $i++) { | |
$field[] = array_fill(0, $W, '.'); | |
} | |
return $field; | |
} | |
function dropBlock(&$field, $H, $W, $h, $w, $x) | |
{ | |
// Find the lowest position the block can land | |
$lowest_position = $H - $h; | |
for ($i = 0; $i <= $H - $h; $i++) { | |
for ($j = 0; $j < $w; $j++) { | |
if ($field[$i + $h - 1][$x + $j] == '#') { | |
$lowest_position = min($lowest_position, $i - 1); | |
break; | |
} | |
} | |
for ($j = 0; $j < $h; $j++) { | |
if ($field[$i + $j][$x] == '#') { | |
$lowest_position = min($lowest_position, $i - 1); | |
break; | |
} | |
} | |
} | |
// Place the block | |
for ($i = 0; $i < $h; $i++) { | |
for ($j = 0; $j < $w; $j++) { | |
$field[$lowest_position + $i][$x + $j] = '#'; | |
} | |
} | |
} | |
function printField($field, $H, $W) | |
{ | |
for ($i = 0; $i < $H; $i++) { | |
echo implode('', $field[$i]) . "\n"; | |
} | |
} | |
// Example usage | |
function runGame($H, $W, $N, $blocks) | |
{ | |
$field = createField($H, $W); | |
foreach ($blocks as $block) { | |
list($h, $w, $x) = $block; | |
dropBlock($field, $H, $W, $h, $w, $x); | |
} | |
printField($field, $H, $W); | |
} | |
// Test cases | |
$tests = [ | |
[ | |
"input" => [ | |
"H" => 7, | |
"W" => 10, | |
"N" => 4, | |
"blocks" => [ | |
[1, 8, 1], | |
[4, 1, 5], | |
[1, 6, 2], | |
[2, 2, 0] | |
] | |
], | |
"expected" => [ | |
"..........", | |
"..######..", | |
".....#....", | |
".....#....", | |
"##...#....", | |
"##...#....", | |
".########." | |
] | |
], | |
[ | |
"input" => [ | |
"H" => 10, | |
"W" => 10, | |
"N" => 9, | |
"blocks" => [ | |
[2, 2, 4], | |
[2, 2, 3], | |
[2, 2, 5], | |
[2, 2, 2], | |
[2, 2, 6], | |
[2, 2, 1], | |
[2, 2, 7], | |
[2, 2, 0], | |
[2, 2, 8] | |
] | |
], | |
"expected" => [ | |
"##......##", | |
"##......##", | |
".##....##.", | |
".##....##.", | |
"..##..##..", | |
"..##..##..", | |
"...####...", | |
"...####...", | |
"....##....", | |
"....##...." | |
] | |
] | |
]; | |
foreach ($tests as $index => $test) { | |
ob_start(); | |
runGame($test["input"]["H"], $test["input"]["W"], $test["input"]["N"], $test["input"]["blocks"]); | |
$output = explode("\n", trim(ob_get_clean())); | |
$expected = $test["expected"]; | |
$result = ($output === $expected) ? "Passed" : "Failed"; | |
echo "Test " . ($index + 1) . ": " . $result . "\n"; | |
} | |
// Reading input from stdin | |
$handle = fopen("php://stdin", "r"); | |
$first_line = fgets($handle); | |
list($H, $W, $N) = array_map('intval', explode(' ', trim($first_line))); | |
$blocks = array(); | |
for ($i = 0; $i < $N; $i++) { | |
$line = fgets($handle); | |
$blocks[] = array_map('intval', explode(' ', trim($line))); | |
} | |
fclose($handle); | |
runGame($H, $W, $N, $blocks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment