您的位置:首页 > 编程语言

《编程之美》 Problem4_10 数字哑谜和回文

2010-08-14 20:15 274 查看
Note: 是该节的问题1

//import java.io.*;
import java.util.*;
public class Problem4_10 {
private int[] ret=new int[9];
private int retTop=0;

public boolean DFS(boolean[] chosen,int depth,int number){
if(depth>0){
if(number%depth!=0)//pruning:
return false;
else if(depth==9)//final state
return true;
else;
}

for(int i=1;i<=9;i++)
if(!chosen[i]){
chosen[i]=true;
number=number*10+i;

if(DFS(chosen,depth+1,number)){
ret[retTop++]=i;
return true;
}else{//restore
chosen[i]=false;
number=number/10;
}
}
return false;//no way!
}

public void printOut(){
while(retTop>0){
System.out.printf("%d",ret[retTop-1]);
retTop--;
}

System.out.println();
}

public static void main(String[] args){
Problem4_10 test=new Problem4_10();

boolean[] chosen=new boolean[10];
Arrays.fill(chosen, false);

test.DFS(chosen, 0, 0);
test.printOut();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: