android数独 解法逻辑
虽然说基本的数独都能解
网上查的那个最难的数独
{0,0,5,3,0,0,0,0,0},
{8,0,0,0,0,0,0,2,0},
{0,7,0,0,1,0,5,0,0},
{4,0,0,0,0,5,3,0,0},
{0,1,0,0,7,0,0,0,6},
{0,0,3,2,0,0,0,8,0},
{0,6,0,5,0,0,0,0,9},
{0,0,4,0,0,0,0,3,0},
{0,0,0,0,0,9,7,0,0}
看来需要添加假设模块呀?~
主要代码
虽然说基本的数独都能解
网上查的那个最难的数独
{0,0,5,3,0,0,0,0,0},
{8,0,0,0,0,0,0,2,0},
{0,7,0,0,1,0,5,0,0},
{4,0,0,0,0,5,3,0,0},
{0,1,0,0,7,0,0,0,6},
{0,0,3,2,0,0,0,8,0},
{0,6,0,5,0,0,0,0,9},
{0,0,4,0,0,0,0,3,0},
{0,0,0,0,0,9,7,0,0}
看来需要添加假设模块呀?~
主要代码
public class Sudu_testActivity extends Activity {
/** Called when the activity is first created. */
int sudu[][]={
{0,0,0,0,0,0,0,0,0},
{9,1,4,0,0,0,0,0,7},
{5,7,0,0,6,0,0,0,0},
{0,9,0,7,0,0,0,2,0},
{0,0,2,6,0,0,0,7,9},
{0,0,0,0,8,0,1,0,6},
{0,3,7,0,0,0,0,0,4},
{0,0,9,0,4,0,0,3,8},
{0,0,0,5,0,0,0,0,0}
};
Piece piece[][];
TextView showView;
Thread thread;
boolean stop=false;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
piece=new Piece[9][9];
for(int i=0;i<9;i++)
for(int j=0;j<9;j++){
piece[i][j]=new Piece(sudu[i][j]);
}
showView=(TextView)findViewById(R.id.show);
btn=(Button)findViewById(R.id.star);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
run();
}
});
show();
//thread=new Thread(this);
//thread.start();
}
public void show(){
String t_str = "";
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
t_str=t_str+" "+piece[i][j].getShow();
}
t_str=t_str+"\n";
}
showView.setText(t_str);
showView.postInvalidate();
}
public void run() {
// TODO Auto-generated method stub
//hang row
for(int i=0;i<9;i++)
for(int j=0;j<9;j++){
int t=piece[i][j].getShow();
if(t==0)
continue;
else
{
//hang row
for(int k=0;k<9;k++){
if(piece[i][k].paichu(t))
{
show();
return ;
}
}
//only
}
}
//lie
for(int i=0;i<9;i++)
for(int j=0;j<9;j++){
int t=piece[i][j].getShow();
if(t==0)
continue;
else
{
//hang row
for(int k=0;k<9;k++){
if(piece[k][j].paichu(t))
{
show();
return;
}
}
}
}
//matrix
for(int i=0;i<9;i++)
for(int j=0;j<9;j++){
int t=piece[i][j].getShow();
if(t==0)
continue;
else
{
//hang row
int row=i/3;
int column=j/3;
for(int k=0;k<9;k++){
if(piece[row*3+k/3][column*3+k%3].paichu(t))
{
show();
return;
}
}
}
}
//only row
for(int i=0;i<9;i++)
for(int o=1;o<=9;o++){
int num=0;
int j=-1;
for(int k=0;k<9;k++)
{
if(piece[i][k].maybe(o)){
num++;
j=k;
}
}
if(num==1)
{
piece[i][j].setShow(o);
show();
return;
}
}
//only column
for(int j=0;j<9;j++)
for(int o=1;o<=9;o++){
int num=0;
int i=-1;
for(int k=0;k<9;k++)
{
if(piece[k][j].maybe(o)){
num++;
i=k;
}
}
if(num==1)
{
piece[i][j].setShow(o);
show();
return;
}
}
//only matrix
for(int k=0;k<9;k++)
{
int row=k/3;
int col=k%3;
for(int o=1;o<=9;o++)
{
int num=0;
int t_i=-1;
int t_j=-1;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(piece[row*3+i][col*3+j].maybe(o)){
num++;
t_i=row*3+i;
t_j=col*3+j;
}
}
if(num==1)
{
piece[t_i][t_j].setShow(o);
show();
return;
}
}
}
}
}
public class Piece {
int show;
int left;
int maybe;
public Piece(){
show=0;
left=9;
maybe=(int) 0x1ff;
}
public Piece(int t){
if(t==0){
show=0;
left=9;
maybe=(int) 0x1ff;
}else{
show=t;
left=0;
maybe=0;
}
}
public void setShow(int t){
if(t==0){
show=0;
left=9;
maybe=(int) 01f;
}else{
show=t;
left=0;
maybe=0;
}
}
public int getShow(){
return show;
}
public boolean paichu(int t){
int x=maybe&1<<(t-1);
if(left!=0&&x!=0)
{
maybe-=(int)1<<(t-1);
left--;
if(left==1){
setShow(leftnum());
return true;
}
else
return false;
}
else return false;
}
public int leftnum(){
int t=maybe;
int i=1;
while(t/2!=0){
t=t/2;
i++;
}
return i;
}
public boolean maybe(int t){
int x=maybe&1<<(t-1);
if(x!=0)
return true;
else
return false;
}
}