pedrotuga
February 26th, 2008, 08:57 AM
I am having trouble using recursive backtracking.
This is a sudoku solver but once it finds a dead end it does not backtrack, it just returns.
The recursive call in inside the for cicle.
I included a 'next' in the code, but i don't know if it properly skis to the next iteration of the for cicle where the current function call is in.
There are some output examples in the code to illustrate the problem.
The function in use have been tested and work properly.
For convinience I uploaded this code in a pastebin to so one can read with syntax highlight.
http://pastebin.com/f275a8fe0
$puzzleraw = "50000000901908075040200060300490650000000000000670 3800307000905085030120200000008";
#500000009
#019080750
#402000603
#004906500
#000000000
#006703800
#307000905
#085030120
#200000008
do "teste.pl";
@puzzle = &put_into_array($puzzleraw);
#solver function
&solve(@puzzle);
sub solve{
&print_puzzle_array(@puzzle);
@puzzle = @_;
if (!have_empty_cells(@puzzle)){
print "Hurray! Puzzle solved!\n";
&print_puzzle_array(@puzzle);
return 1;
}
$empty_cell_position = &find_first_empty_cell(@puzzle);
$puzzle[0] = $empty_cell_position; #passed allong with the puzzle in the same array just to simplify
@cell_possibilities = &get_possibilities(@puzzle);
if ($#cell_possibilities == -1){next;} #can i use next in here to skip to the next iteration of the for cicle i am in?
for $possible_value (@cell_possibilities){
$puzzle[$empty_cell_position] = $possible_value;
solve(@puzzle);
}
}
################################################## ####################
#here goes a sample output
p@p-laptop:~/Desktop/perlscripts$ perl solver.pl
=========
500000009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
=========
530000009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
=========
538000009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
=========
538100009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
=========
538120009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
=========
538124009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
p@p-laptop:~/Desktop/perlscripts$
This is a sudoku solver but once it finds a dead end it does not backtrack, it just returns.
The recursive call in inside the for cicle.
I included a 'next' in the code, but i don't know if it properly skis to the next iteration of the for cicle where the current function call is in.
There are some output examples in the code to illustrate the problem.
The function in use have been tested and work properly.
For convinience I uploaded this code in a pastebin to so one can read with syntax highlight.
http://pastebin.com/f275a8fe0
$puzzleraw = "50000000901908075040200060300490650000000000000670 3800307000905085030120200000008";
#500000009
#019080750
#402000603
#004906500
#000000000
#006703800
#307000905
#085030120
#200000008
do "teste.pl";
@puzzle = &put_into_array($puzzleraw);
#solver function
&solve(@puzzle);
sub solve{
&print_puzzle_array(@puzzle);
@puzzle = @_;
if (!have_empty_cells(@puzzle)){
print "Hurray! Puzzle solved!\n";
&print_puzzle_array(@puzzle);
return 1;
}
$empty_cell_position = &find_first_empty_cell(@puzzle);
$puzzle[0] = $empty_cell_position; #passed allong with the puzzle in the same array just to simplify
@cell_possibilities = &get_possibilities(@puzzle);
if ($#cell_possibilities == -1){next;} #can i use next in here to skip to the next iteration of the for cicle i am in?
for $possible_value (@cell_possibilities){
$puzzle[$empty_cell_position] = $possible_value;
solve(@puzzle);
}
}
################################################## ####################
#here goes a sample output
p@p-laptop:~/Desktop/perlscripts$ perl solver.pl
=========
500000009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
=========
530000009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
=========
538000009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
=========
538100009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
=========
538120009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
=========
538124009
019080750
402000603
004906500
000000000
006703800
307000905
085030120
200000008
=========
p@p-laptop:~/Desktop/perlscripts$