<head>
<script>
delayTime=10;
board=[];
maplistc = [];
maplistr = [];
maplistk = [];
emptylist = [];
notEmptyList={};
run=false;
function refresh(){
if(document.getElementById("t")!=null)
document.getElementById("t").remove();
if(document.getElementById("b1")!=null)
document.getElementById("b1").remove();
if(document.getElementById("b2")!=null)
document.getElementById("b2").remove();
if(document.getElementById("s")!=null)
document.getElementById("s").remove();
document.write("<table id='t'>");
for(var i=0;i<board.length;i++){
document.write("<tr>");
for(var j=0;j<board[i].length;j++){
var clazz = notEmptyList[i+" "+j]?
"width:50px;height:50px;border:1px solid black;line-height:50px;text-align:center;background-color:yellow;"
:
"width:50px;height:50px;border:1px solid black;line-height:50px;text-align:center;";
if(board[i][j]=="."){
document.write("<td style='"+clazz+"'></td>");
}else{
document.write("<td style='"+clazz+"'>"+board[i][j]+"</td>");
}
}
document.write("</tr>");
}
document.write("</table>");
if(!run){
document.write("<select id='s'>");
for(var i=0;i<=17;i++){
document.write("<option>"+i+"</option>");
}
document.write("</select>");
document.write("<button id='b1' onclick='reset()'>刷新</button> <button id='b2' onclick='startRun()'>开始</button>");
}
}
function getIndex(i, j) {
if (i >= 0 && i <= 2 && j >= 0 && j <= 2) {
return 0;
} else if (i >= 0 && i <= 2 && j >= 3 && j <= 5) {
return 1;
} else if (i >= 0 && i <= 2 && j >= 6 && j <= 8) {
return 2;
} else if (i >= 3 && i <= 5 && j >= 0 && j <= 2) {
return 3;
} else if (i >= 3 && i <= 5 && j >= 3 && j <= 5) {
return 4;
} else if (i >= 3 && i <= 5 && j >= 6 && j <= 8) {
return 5;
} else if (i >= 6 && i <= 8 && j >= 0 && j <= 2) {
return 6;
} else if (i >= 6 && i <= 8 && j >= 3 && j <= 5) {
return 7;
} else {
return 8;
}
}
function solveSudoku() {
maplistc = [{},{},{},{},{},{},{},{},{}];
maplistr = [{},{},{},{},{},{},{},{},{}];
maplistk = [{},{},{},{},{},{},{},{},{}];
emptylist = [];
for (var i = 0; i < 9; i++) {
for(var j=1;j<=9;j++){
maplistc[i][j]=0;
maplistr[i][j]=0;
maplistk[i][j]=0;
}
}
for (var i = 0; i < board.length; i++) {
for (var j = 0; j < board[i].length; j++) {
if (board[i][j] != '.') {
maplistc[i][board[i][j]]=1;
maplistr[j][board[i][j]]=1;
maplistk[getIndex(i, j)][board[i][j]]=1;
} else {
emptylist.push([ i, j ]);
}
}
}
//insert(0, emptylist);
delayInsert(0,emptylist);
}
function delayInsert(index,list){
var arr = list[index];
var i = arr[0], j = arr[1], n=1;;
if(board[i][j]=="."){
n=1;
}else if(board[i][j]<9){
n=board[i][j]+1;
}else{
n=-1;
}
if(n!=-1){
board[i][j] = n;
if (
maplistc[i][n]==0
&& maplistr[j][n]==0
&& maplistk[getIndex(i, j)][n]==0) {
maplistc[i][n]=1;
maplistr[j][n]=1;
maplistk[getIndex(i, j)][n]=1;
if (index >= list.length - 1) {
console.log("over");
run=false;
}else{
setTimeout(function(){
delayInsert(index+1,emptylist);
},delayTime);
}
}else{
setTimeout(function(){
delayInsert(index,emptylist);
},delayTime);
}
}else{
board[i][j] = ".";
arr = list[index-1];
i=arr[0];
j=arr[1];
n=board[i][j]
maplistc[i][n]=0;
maplistr[j][n]=0;
maplistk[getIndex(i, j)][n]=0;
setTimeout(function(){
delayInsert(index-1,emptylist);
},delayTime);
}
refresh();
}
function insert( index, list) {
var arr = list[index];
var i = arr[0], j = arr[1];
for (var n = 1; n <= 9; n++) {
if (
maplistc[i][n]==0
&& maplistr[j][n]==0
&& maplistk[getIndex(i, j)][n]==0) {
board[i][j] = n+"";
maplistc[i][n]=1;
maplistr[j][n]=1;
maplistk[getIndex(i, j)][n]=1;
//refresh();
if (index >= list.length - 1) {
return true;
} else {
if (insert(index + 1, list, maplistc, maplistr, maplistk)) {
return true;
} else {
board[i][j] = ".";
maplistc[i][n]=0;
maplistr[j][n]=0;
maplistk[getIndex(i, j)][n]=0;
}
}
}
}
return false;
}
function createBoard(nmb) {
notEmptyList={};
var maplistc = [{},{},{},{},{},{},{},{},{}];
var maplistr = [{},{},{},{},{},{},{},{},{}];
var maplistk = [{},{},{},{},{},{},{},{},{}];
for (var i = 0; i < 9; i++) {
for(var j=1;j<=9;j++){
maplistc[i][j]=0;
maplistr[i][j]=0;
maplistk[i][j]=0;
}
}
var result = [];
for(var i=0;i<9;i++) {
result[i]=['.','.','.','.','.','.','.','.','.'];
}
//var nmb =0;
while(nmb>0) {
var x = Math.floor(Math.random()*9);
var y = Math.floor(Math.random()*9);
if(result[x][y]=='.') {
var tnmb = Math.floor(Math.random()*9)+1;
if(maplistc[x][tnmb]==0
&&maplistr[y][tnmb]==0
&&maplistk[getIndex(x, y)][tnmb]==0
) {
result[x][y]=tnmb+"";
maplistc[x][tnmb]=1;
maplistr[y][tnmb]=1;
maplistk[getIndex(x, y)][tnmb]=1;
nmb--;
notEmptyList[x+" "+y]=1;
}
}
}
return result;
}
board = createBoard();
refresh();
//solveSudoku(board);
function reset(){
var nmb = document.getElementById('s').value;
board = createBoard(nmb);
refresh();
}
function startRun(){
solveSudoku();
run=true;
}
</script>
</head>
<body>
</body>