D
Dennis Dahn
Hello,
I have a general question, is it possible to turn every rekursiv algorithm
into an iterativ one?
for example this one:
static boolean solve(int i, int j, int[][] cells) {
if (i == 9) {
i = 0;
if (++j == 9)
return true;
}
if (cells[j] != 0) // skip filled cells
return solve(i+1,j,cells);
<- here is the rekursiv call
for (int val = 1; val <= 9; ++val) {
if (legal(i,j,val,cells)) {
cells[j] = val;
if (solve(i+1,j,cells))
<- and here!
return true;
}
}
cells[j] = 0; // reset on backtrack
return false;
}
thank you very much for your help!
regards,
dennis
I have a general question, is it possible to turn every rekursiv algorithm
into an iterativ one?
for example this one:
static boolean solve(int i, int j, int[][] cells) {
if (i == 9) {
i = 0;
if (++j == 9)
return true;
}
if (cells[j] != 0) // skip filled cells
return solve(i+1,j,cells);
<- here is the rekursiv call
for (int val = 1; val <= 9; ++val) {
if (legal(i,j,val,cells)) {
cells[j] = val;
if (solve(i+1,j,cells))
<- and here!
return true;
}
}
cells[j] = 0; // reset on backtrack
return false;
}
thank you very much for your help!
regards,
dennis