- Timestamp:
- 06/12/08 21:36:49 (7 months ago)
- Files:
-
- 1 modified
-
ext/Sudoku/lib/Sudoku.pm (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ext/Sudoku/lib/Sudoku.pm
r16634 r20772 87 87 $!delimiter ~= "\n"; 88 88 89 for (0 .. $!length - 1) -> my$i {89 for (0 .. $!length - 1) > $i { 90 90 #columns: 91 91 push @!constraint, [($i xx $!length) Z (0 .. $!length - 1)]; … … 128 128 # $s.chars == $!length*$!length; 129 129 # } 130 for (0 .. $!length*$!length-1) -> my$i {130 for (0 .. $!length*$!length-1) -> $i { 131 131 if +$s.substr($i, 1) { 132 132 self.set_item(+$s.substr($i, 1), $i % $!length, int($i / $!length)); … … 159 159 # constraint propagation 160 160 # no more numbers are allowed where the current number is set 161 for (0 .. $!length - 1) -> my$i {161 for (0 .. $!length - 1) -> $i { 162 162 @!allowed[$x][$y][$i] = 0; 163 163 } 164 164 165 165 # propagation in the rest: 166 for @!constraint -> my$c {166 for @!constraint -> $c { 167 167 my $in = 0; 168 for @$c -> my$cell {168 for @$c -> $cell { 169 169 if $cell[0] == $x and $cell[1] == $y { 170 170 $in = 1; … … 174 174 175 175 if $in { 176 for @$c -> my$cell {176 for @$c -> $cell { 177 177 @!allowed[$cell[0]][$cell[1]][$elem-1] = 0; 178 178 } … … 286 286 method simple_solve1($self:) { 287 287 my $success = 0; 288 for (0 .. $!length - 1) -> my$x {289 for (0 .. $!length -1) -> my$y {288 for (0 .. $!length - 1) -> $x { 289 for (0 .. $!length -1) -> $y { 290 290 my $count = 0; 291 291 my $pointer = 0; 292 for (0 .. $!length - 1) -> my$num {292 for (0 .. $!length - 1) -> $num { 293 293 if @!allowed[$x][$y][$num - 1] { 294 294 $count++; … … 316 316 my $success = 0; 317 317 318 for @!constraint -> my$c {319 for (1 .. $!length)-> my$num {318 for @!constraint -> $c { 319 for (1 .. $!length)-> $num { 320 320 my $x; 321 321 my $y; 322 322 my $count = 0; 323 for @$c -> my$tupel {323 for @$c -> $tupel { 324 324 if @!allowed[$tupel[0]][$tupel[1]][$num - 1] { 325 325 $count++; … … 343 343 344 344 method is_solved { 345 for (0 .. $!length - 1) -> my$x {346 for (0 .. $!length - 1) -> my$y {345 for (0 .. $!length - 1) -> $x { 346 for (0 .. $!length - 1) -> $y { 347 347 return 0 if @.field[$x][$y] == 0; 348 348 } … … 359 359 method backtrack($self:) { 360 360 # say "backtracking..."; 361 for (0 .. $!length - 1) -> my$x {362 for (0 .. $!length - 1) -> my$y {361 for (0 .. $!length - 1) -> $x { 362 for (0 .. $!length - 1) -> $y { 363 363 if @.field[$x][$y] == 0 { 364 364 # found an empty positon, let's backtrack here! 365 365 # say "Backtracking at ($x, $y)"; 366 for (1 .. $!length) -> my$num {366 for (1 .. $!length) -> $num { 367 367 if $self.is_allowed($num, $x, $y) { 368 368 # say "found";
