in reply to [code challenge] knights move
#!/usr/bin/perl # http://perlmonks.org/?node_id=1131329 [code challenge] knights move use strict; use warnings; my @queue = my $start = <<endofdata; # find backwards OOO ... ... XXX endofdata tr/XO/OX/ for my $end = $start; my %from; my $count = 0; my @moves = qw(0-5 0-8 1-6 1-8 2-1 2-6 4-5 4-8 5-6 5-8 6-1 6-6 8-5 10- +1); while( $_ = shift @queue and $_ ne $end ) # breadth first search { for my $move ( @moves ) { my ($skip, $gap) = split /-/, $move; for my $new ( s/^.{$skip}\K(\w)(.{$gap})\./.$2$1/sr, s/^.{$skip}\K\.(.{$gap})(\w)/$2$1./sr, ) { if( $new ne $_ && !$from{$new} ) { $from{$new} = $_; # keep backtrace push @queue, $new; } } } } while( $from{$end} ) # prints in reverse order of found { $count++; print " $count\n", $end; $end eq $start and last; $end = $from{$end}; # go back a step }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: [code challenge] knights move
by LanX (Saint) on Jun 21, 2015 at 22:30 UTC | |
by Anonymous Monk on Jun 21, 2015 at 22:47 UTC | |
by LanX (Saint) on Jun 21, 2015 at 23:04 UTC | |
by Anonymous Monk on Jun 21, 2015 at 23:15 UTC |