duanxn
03-27-2006, 01:25 PM
//Create two arrays:
// a: Holds the puzzle value.
// b: Holds the predefined flag(non-zero)
a = new Array(0,0);
b = "060590004"
+"005004302"
+"290700100"
+"000017238"
+"400000006"
+"378920000"
+"002001049"
+"507600800"
+"800035010";
for (i = 0; i < b.length; i++)
{ a[i] = parseInt(b[i]);
}
//Loop through each none predefined cell
L: for (k = FindNext(-1); k<b.length; k++)
{ while (a[k]+= 1 )
{ if (a[k] == 10)
{ if ((k = GoBack(k))== -1)
{ document.write("Cannot find solution!!"); break L;
} }
else
{ if (Validate(k))
{ if ((k = FindNext(k)) == -1) // solution found !
{ document.write("Found solution"); document.write(a); break L;
} } } } }
// Sets the pointer to the next non-predefined cell after cell i,
// returns -1 when goes beyond the boundary.
function FindNext(i)
{ for(j=1;;j++)
{ if (i+j== b.length ) return -1;
if (b[i+j]==0) return i+j;
} }
//Sets the pointer back to the last increasable non-predefined cell,
// meanwhile, sets the values of the passed cells to zero,
// returns -1 when goes beyond the boundary.
function GoBack(i)
{ a[i] = 0;
while (--i >= 0)
{ if (b[i]==0)
{ if (a[i]<9)
{ return i;
} else
{ a[i] = 0;
} } }
return -1;
}
//Validate the cell x
function Validate(x)
{ Xr = Math.floor(x/9)*9; //Row base
Xc = x % 9; //Column base
Xb = (Math.floor(x / 27)) * 27 + ( (Math.floor((x % 9)/3)) *3 ); //Block base
for (i = 0; i < 3; i++)
{ for (j = 0; j < 3; j++)
{ if ((((Xr+i*3+j) != x) && (a[Xr+i*3+j] ==a[x]))
|| (((Xc+(i*3+j)*9)!= x) && (a[Xc+(i*3+j)*9] ==a[x]))
|| (((Xb+i*9 + j) != x) && (a[Xb+i*9 + j] ==a[x])))
return false;
} }
return true;
}
// a: Holds the puzzle value.
// b: Holds the predefined flag(non-zero)
a = new Array(0,0);
b = "060590004"
+"005004302"
+"290700100"
+"000017238"
+"400000006"
+"378920000"
+"002001049"
+"507600800"
+"800035010";
for (i = 0; i < b.length; i++)
{ a[i] = parseInt(b[i]);
}
//Loop through each none predefined cell
L: for (k = FindNext(-1); k<b.length; k++)
{ while (a[k]+= 1 )
{ if (a[k] == 10)
{ if ((k = GoBack(k))== -1)
{ document.write("Cannot find solution!!"); break L;
} }
else
{ if (Validate(k))
{ if ((k = FindNext(k)) == -1) // solution found !
{ document.write("Found solution"); document.write(a); break L;
} } } } }
// Sets the pointer to the next non-predefined cell after cell i,
// returns -1 when goes beyond the boundary.
function FindNext(i)
{ for(j=1;;j++)
{ if (i+j== b.length ) return -1;
if (b[i+j]==0) return i+j;
} }
//Sets the pointer back to the last increasable non-predefined cell,
// meanwhile, sets the values of the passed cells to zero,
// returns -1 when goes beyond the boundary.
function GoBack(i)
{ a[i] = 0;
while (--i >= 0)
{ if (b[i]==0)
{ if (a[i]<9)
{ return i;
} else
{ a[i] = 0;
} } }
return -1;
}
//Validate the cell x
function Validate(x)
{ Xr = Math.floor(x/9)*9; //Row base
Xc = x % 9; //Column base
Xb = (Math.floor(x / 27)) * 27 + ( (Math.floor((x % 9)/3)) *3 ); //Block base
for (i = 0; i < 3; i++)
{ for (j = 0; j < 3; j++)
{ if ((((Xr+i*3+j) != x) && (a[Xr+i*3+j] ==a[x]))
|| (((Xc+(i*3+j)*9)!= x) && (a[Xc+(i*3+j)*9] ==a[x]))
|| (((Xb+i*9 + j) != x) && (a[Xb+i*9 + j] ==a[x])))
return false;
} }
return true;
}