in reply to Google Code Jam 2019 Round 1A Problem 1: Pylons
Crummy problem :( ----- (because random)
This is a permutation problem with a weak ordering requirement.
"shuffle" the grid indexes, about half the time always taking the first non-conflict from the array of empty indexes gives a valid order, if not, reshuffle till it works.
I am seeing on average slightly less than one reshuffle per grid solution over all the grids.
Processes all 361 grids in under 2 seconds total typically.
( slight cheat: All IMPOSSIBLEs are hard coded (HINT: there are very few) :)
It's not in final submit form because I wanted to test all grid sizes.
#!/usr/bin/perl # https://codingcompetitions.withgoogle.com/codejam/round/000000000005 +1635/0000000000104e03 use strict; use warnings; use List::Util qw( first shuffle ); my $w; my $case = 0; for my $row ( 2 .. 20 ) # testing all sizes, needs change to read i +nput file { for my $col ( 2 .. 20 ) { $case++; if( $row == 2 && $col < 5 || $row < 5 && $col == 2 || $row == 3 && $col == 3) { print "Case #$case: IMPOSSIBLE\n"; # h +ard coded next; } $w = $col; my @pylons = 0; # first pylon in upper le +ft corner my @empty = shuffle 1 .. $row * $col - 1; while( @empty ) { if( my $try = first { ! conflict($pylons[-1], $_) } @empty ) { push @pylons, $try; @empty = grep $_ != $try, @empty; } else { push @empty, shuffle splice @pylons, 1; # +reshuffle } } print "Case #$case: POSSIBLE\n"; # uncomment next line to print grid locations #print +(int 1 + $_ / $w) . ' ' . (1 + $_ % $w) . "\n" for @pylons +; } } sub conflict { my ($x, $y) = @_; my ($r, $c, $rr, $cc) = (int $x / $w, $x % $w, int $y / $w, $y % $w) +; $r == $rr || $c == $cc || $r + $c == $rr + $cc || $r - $c == $rr - $ +cc; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Google Code Jam 2019 Round 1A Problem 1: Pylons
by LanX (Saint) on Apr 15, 2019 at 17:01 UTC | |
by tybalt89 (Monsignor) on Apr 15, 2019 at 17:43 UTC | |
by LanX (Saint) on Apr 15, 2019 at 19:24 UTC | |
by tybalt89 (Monsignor) on Apr 15, 2019 at 19:44 UTC | |
by LanX (Saint) on Apr 15, 2019 at 20:03 UTC | |
|