|
发表于 2008-1-4 15:14:02
|
显示全部楼层
这里有个js版本的解题器:
http://www.sudoku.name/sudoku-solver/cn
- <script language=javascript>
- function solve_puzzle()
- {
- //check fields
- var flag=0;
- for (i=1;i<82;i++)
- {
-
- if (document.getElementById('c'+i).value>0)
- {
- flag=1;
- //alert(flag);
- break;
- }
- }
- if (flag==0)//no numbers
- {
- alert("请输入数独迷题的起始数字");
- return;
- }
- else//initial numbers in place submit
- {
- document.getElementById('solve').value='1';
- form1.target='_top';
- document.form1.action='';
- document.form1.submit();
- }
- }
- function change_puzzle()
- {
- document.getElementById('change').value='1';
- form1.target='_top';
- document.form1.action='';
- document.form1.submit();
- }
- //all cells read only
- //place initial numbers&mark background cells
- shown_arr1=new Array(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
- function repaint()
- {
- //repaint cells
- for (i=1;i<82;i++)//insert initial numbers repaint cells
- {
- //if ($_POST['c'.$i]!="") echo $_POST['c'.$i];
- if (shown_arr1[i-1]>0)
- {
- document.getElementById('c'+i).value=shown_arr1[i-1];
- document.getElementById('cell'+i).bgColor= '#E5E5E5';
- }
- }
- }
- //initializing vars
- var col_counter=1;//1-9
- var row_counter=1;//1-9
- var region_counter=1;//1-9
- var cell_counter=0;//0-82
- var direction=1;//1||-1
- puzzle_arr=new Array(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
- initial_puzzle_arr=new Array(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
-
- function check_grid()//check fields!!!
- {
- empty=0;
- legal=0;
- //alert('yes');
- //prepare shown cells array
- //check rows
- for (i=1;i<10;i++)
- {
- tmp_arr=new Array(0,0,0,0,0,0,0,0,0);
- start=1+((i-1)*9);
- finish=((i-1)*9)+10;
- for (j=start;j<finish;j++)//check cell of row
- {
- tmp=document.getElementById('c'+j).value;
- if (tmp!="")//disrigard empty cells
- {
- //alert(tmp);
- if (tmp_arr[tmp-1]==0) tmp_arr[tmp-1]=tmp;
- else
- {
- //error
- legal=1;
- //mark duplicaned cells
-
- //return;// false;
- break;
- }
- }
- else empty=1;
- //t+=1;
- }
- //t+=9;
- }//end check rows
-
- //check columns
- for (i=1;i<10;i++)
- {
- //i= column start
- tmp_arr=new Array(0,0,0,0,0,0,0,0,0);
- start=i;
- finish=73+i;
- for (j=start;j<finish;j+=9)//check cell of column
- {
- tmp=document.getElementById('c'+j).value;
- if (tmp!="")//disrigard empty cells
- {
- //alert(tmp);
- if (tmp_arr[tmp-1]==0) tmp_arr[tmp-1]=tmp;
- else
- {
- //error
- legal=1;
- //mark duplicaned cells
-
- //return;// false;
- break;
- }
- }
- else empty=1;
- }
- }//end column check
-
- //check regions
- for (i=1;i<10;i++)
- {
- tmp_region=new Array(1,4,7,28,31,34,55,58,61);
- tmp_arr=new Array(0,0,0,0,0,0,0,0,0);
- start=tmp_region[i-1];
- finish=tmp_region[i-1]+21;
- for (j=start;j<finish;j+=9)//row
- {
- for (j1=j;j1<j+3;j1++)//column
- {
- tmp=document.getElementById('c'+j1).value;
- if (tmp!="")//disrigard empty cells
- {
- //alert(tmp);
- if (tmp_arr[tmp-1]==0) tmp_arr[tmp-1]=tmp;
- else
- {
- //error
- legal=1;
- //mark duplicaned cells
-
-
- //return;// false;
- break;
- }
- }
- else empty=1;
- }
- }
- }//end checking regions
-
- if (legal==1)
- {
- alert('这个数独迷题无法解答');
- return false;
- }
- return true;
- }
- function solution()
- {
-
- if (document.getElementById("mark").checked==false)//the stop feature
- {
- if (initial_puzzle_arr[cell_counter]==0)
- {
- //if (puzzle_arr[cell_counter]<9)
- while (puzzle_arr[cell_counter]<10)
- {
- puzzle_arr[cell_counter]+=1;
- if (puzzle_arr[cell_counter]==10)
- {
- puzzle_arr[cell_counter]=0;
- tmp=cell_counter+1;
- document.getElementById('c'+tmp).value='';
- direction=-1;
- break;
- }
- //alert(puzzle_arr[cell_counter]);
- tmp=cell_counter+1;
- document.getElementById('c'+tmp).value=puzzle_arr[cell_counter];//shown puzzle
- //check_legal
- legal=0;
-
- //legal row
- //alert('initial '+legal);
- tmp_start=((row_counter-1)*9);//+1;
- tmp_end=((row_counter-1)*9)+9;//10;
- for (i=tmp_start;i<tmp_end;i++)
- {
- if ((puzzle_arr[cell_counter]==puzzle_arr[i])&&(cell_counter!=i))
- {
- legal=1;
- break;
- }
- }
- //alert('row '+legal);
-
- //legal col
- if (legal==0)
- {
- for (i=col_counter;i<col_counter+73;i+=9)
- {
- if ((puzzle_arr[cell_counter]==puzzle_arr[i-1])&&(cell_counter!=i-1))
- {
- legal=1;
- break;
- }
- }
- }
- //alert('col '+legal);
- //check regions
- if (legal==0)
- {
- tmp_region=new Array(1,4,7,28,31,34,55,58,61);//cells numbers
- i=region_counter;
- start=tmp_region[i-1];
- finish=tmp_region[i-1]+21;
- //alert(start+' '+finish);
- for (j=start;j<finish;j+=9)//row
- {
- for (j1=j;j1<j+3;j1++)//column
- {
- if ((puzzle_arr[cell_counter]==puzzle_arr[j1-1])&&(cell_counter!=j1-1))
- {
- legal=1;
- break;
-
- }
- }
- if (legal==1) break;
- }
- }
-
- //alert('region '+legal);
- if (legal==0)
- {
- direction=1;
- break;
- }
- }
- }
-
- cell_counter+=direction;//next cell
- cell_counter1=cell_counter+1;
- //update counters
- //row number
- //cell_counter++;//for convinience
- if ((cell_counter1>0)&&(cell_counter1<10)) row_counter=1;
- else if ((cell_counter1>9)&&(cell_counter1<19)) row_counter=2;
- else if ((cell_counter1>18)&&(cell_counter1<28)) row_counter=3;
- else if ((cell_counter1>27)&&(cell_counter1<37)) row_counter=4;
- else if ((cell_counter1>36)&&(cell_counter1<46)) row_counter=5;
- else if ((cell_counter1>45)&&(cell_counter1<55)) row_counter=6;
- else if ((cell_counter1>54)&&(cell_counter1<64)) row_counter=7;
- else if ((cell_counter1>63)&&(cell_counter1<73)) row_counter=8;
- else if ((cell_counter1>72)&&(cell_counter1<82)) row_counter=9;
- //column number
- col_counter=cell_counter1-(9*(row_counter-1));
- //region number
- region_counter=((Math.ceil(row_counter/3)-1)*3)+Math.ceil(col_counter/3);
- //cell_counter--;//for convinience
- if (cell_counter<0)
- {
- alert('这个数独迷题无法解答');
- return;
- }
- else if (cell_counter>80)
- {
- alert('迷题已经成功解答');
- return;
- }
- }//stop feature
- if ((cell_counter1>0) || (cell_counter1<82)) window.setTimeout(solution, 5);
-
- }
- function delete_marking(id)
- {
- if (document.getElementById('c'+id).value.length==0)
- {
- document.getElementById('cell'+id).bgColor= '#FFFFFF';
-
- }
- //look for red flags, there can be only 2
- }
- function move(event,id)
- {
- var key;
- var keychar;
- if (window.event) key = window.event.keyCode;
- else if (e) key = e.which;
- else return true;
- //left=37
- //right=39
- //up=38
- //down=40
- if (key==37)//left
- {
- if ((id==1)||(id==10)||(id==19)||(id==28)||(id==37)||(id==46)||(id==55)||(id==64)||(id==73))
- {
- //don't move left
- }
- else
- {
- //delete after checking
- //check how to act on readonly field
- //move 1 cell left
- document.getElementById('c'+(id-1)).focus();
- //document.getElementById('c'+(id-1)).select();
- }
- }
- else if (key==39)//right
- {
- if ((id==9)||(id==18)||(id==27)||(id==36)||(id==45)||(id==54)||(id==63)||(id==72)||(id==81))
- {
- //don't move left
- }
- else
- {
- //move 1 cell right
- tmp_id=id;
- tmp_id++;
- //alert(tmp_id);
- document.getElementById('c'+tmp_id).focus();
- }
- }
- else if (key==38)//up
- {
- if ((id==1)||(id==2)||(id==3)||(id==4)||(id==5)||(id==6)||(id==7)||(id==8)||(id==9))
- {
- //don't move left
- }
- else
- {
- //move 1 cell right
- tmp_id=id;
- tmp_id-=9;
- //alert(tmp_id);
- document.getElementById('c'+tmp_id).focus();
- }
- }
- else if (key==40)//down
- {
- if ((id==73)||(id==74)||(id==75)||(id==76)||(id==77)||(id==78)||(id==79)||(id==80)||(id==81))
- {
- //don't move left
- }
- else
- {
- //move 1 cell right
-
- //tmp_id=id;
- tmp_id=parseInt(id);
- tmp_id=tmp_id+9;
- //alert(tmp_id);
- document.getElementById('c'+tmp_id).focus();
- }
- }
- }
- function valid_number(event,tmp_cell)
- {
- var key;
- var keychar;
-
- if (window.event) key = window.event.keyCode;
- else if (e) key = e.which;
- else return true;
- //save_change('save_list');//when keypressed inside cell make update available
- //alert("yeas "+key);
- keychar = String.fromCharCode(key);
-
- if ((("123456789").indexOf(keychar) > -1))
- {
- document.getElementById(tmp_cell).bgColor= '#E5E5E5';
- return true;
- }
- else return false;
-
- }
- </script>
复制代码 |
|