刚学java没多久,用java写了个求解数独的程序,编译没问题,就是运行没结果,请大家指教一下,感激不尽~~~~~~~~~~~~~~~~
package sudoku;public class Sudoku {
int sd[][] = {
{ 0, 0, 0, 0, 0, 0, 5, 0, 0 },
{ 3, 0, 2, 0, 7, 0, 9, 1, 0 },
{ 6, 0, 0, 9, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 2, 6 },
{ 0, 2, 0, 3, 0, 0, 1, 5, 9 },
{ 7, 9, 0, 6, 0, 5, 0, 8, 0 },
{ 1, 0, 9, 7, 0, 0, 0, 0, 0 },
{ 4, 5, 0, 0, 0, 0, 2, 3, 0 },
{ 0, 3, 8, 4, 5, 0, 6, 0, 0 }, };
int set[] = new int[9];
int wait = 0;
int has = 0; // 重置set数组
void reSet() {
int i;
for ( i = 0; i < 9; i++) {
set[i]=i+1;
}
has = 9;
} // 统计一开始有多少待填入数字的位置
void countWait() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (sd[i][j] == 0) {
wait++;
}
}
}
} // 判断传入的n这个数是否已经存在于可选填入的数组中
//存在则删除set中的对应数,has--
boolean isContain(int n) {
for (int i = 0; i < 9; i++) {
if (set[i] == n) {
set[i] = 0;
has--;
return true;
}
}
return false;
} // 求解这个sd这个数独
void Solution() {
countWait(); // 统计有多少待填入的数的位置
reSet(); // 重置待填入数组
while (wait != 0) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
reSet(); // 每次判断完,重置可填入数的数组
if (sd[i][j] == 0) { // 如果位置为可填入位置
for (int x = 0; x < 9; x++) { // 检验同列中是否有已存在于set中的元素
isContain(sd[i][x]); // 若存在则删除set中的对应元素
}
for (int x = 0; x < 9; x++) { // 检验同行中是否有已存在于set中的元素
isContain(sd[x][j]); // 若存在则删除set中的对应元素
} if (i >= 0 && i <= 2 && j >= 0 && j <= 2) { // 判断sd[i][j]是否处于第一个九宫格内
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
isContain(sd[x][y]);
}
}
} if (i >= 0 && i <= 2 && j >= 3 && j <= 5) { // 判断sd[i][j]是否处于第二个九宫格内
for (int x = 0; x < 3; x++) {
for (int y = 3; y < 6; y++) {
isContain(sd[x][y]);
}
}
}
if (i >= 0 && i <= 2 && j >= 6 && j <= 8) { // 判断sd[i][j]是否处于第三个九宫格内
for (int x = 0; x < 3; x++) {
for (int y = 6; y < 9; y++) {
isContain(sd[x][y]);
}
}
} if (i >= 3 && i <= 5 && j >= 0 && j <= 2) { // 判断sd[i][j]是否处于第四个九宫格内
for (int x = 3; x < 6; x++) {
for (int y = 0; y < 3; y++) {
isContain(sd[x][y]);
}
}
}
if (i >= 3 && i <= 5 && j >= 3 && j <= 5) { // 判断sd[i][j]是否处于第五个九宫格内
for (int x = 3; x < 6; x++) {
for (int y = 3; y < 6; y++) {
isContain(sd[x][y]);
}
}
}
if (i >= 3 && i <= 5 && j >= 6 && j <= 8) { // 判断sd[i][j]是否处于第六个九宫格内
for (int x = 3; x < 6; x++) {
for (int y = 6; y < 9; y++) {
isContain(sd[x][y]);
}
}
} if (i >= 6 && i <= 8 && j >= 0 && j <= 2) { // 判断sd[i][j]是否处于第七个九宫格内
for (int x = 6; x < 9; x++) {
for (int y = 0; y < 3; y++) {
isContain(sd[x][y]);
}
}
}
if (i >= 6 && i <= 8 && j >= 3 && j <= 5) { // 判断sd[i][j]是否处于第八个九宫格内
for (int x = 6; x < 9; x++) {
for (int y = 3; y < 6; y++) {
isContain(sd[x][y]);
}
}
}
if (i >= 6 && i <= 8 && j >= 6 && j <= 8) { // 判断sd[i][j]是否处于第九个九宫格内
for (int x = 6; x < 9; x++) {
for (int y = 6; y < 9; y++) {
isContain(sd[x][y]);
}
}
}
if (has == 1) {
for (int x = 0; x < 9; x++) {
if (set[x] != 0) {
sd[i][j] = set[x];
wait--;
break;
}
}
}
}
}
}
}
} //输出这个数独
void display() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(" "+sd[i][j]+" ");
if(j%3==0)
System.out.print("|");
}
if(i%3==0)
System.out.println("---------------------------------------------------");
else
System.out.println();
}
}
}
package sudoku;public class Sudoku {
int sd[][] = {
{ 0, 0, 0, 0, 0, 0, 5, 0, 0 },
{ 3, 0, 2, 0, 7, 0, 9, 1, 0 },
{ 6, 0, 0, 9, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 2, 6 },
{ 0, 2, 0, 3, 0, 0, 1, 5, 9 },
{ 7, 9, 0, 6, 0, 5, 0, 8, 0 },
{ 1, 0, 9, 7, 0, 0, 0, 0, 0 },
{ 4, 5, 0, 0, 0, 0, 2, 3, 0 },
{ 0, 3, 8, 4, 5, 0, 6, 0, 0 }, };
int set[] = new int[9];
int wait = 0;
int has = 0; // 重置set数组
void reSet() {
int i;
for ( i = 0; i < 9; i++) {
set[i]=i+1;
}
has = 9;
} // 统计一开始有多少待填入数字的位置
void countWait() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (sd[i][j] == 0) {
wait++;
}
}
}
} // 判断传入的n这个数是否已经存在于可选填入的数组中
//存在则删除set中的对应数,has--
boolean isContain(int n) {
for (int i = 0; i < 9; i++) {
if (set[i] == n) {
set[i] = 0;
has--;
return true;
}
}
return false;
} // 求解这个sd这个数独
void Solution() {
countWait(); // 统计有多少待填入的数的位置
reSet(); // 重置待填入数组
while (wait != 0) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
reSet(); // 每次判断完,重置可填入数的数组
if (sd[i][j] == 0) { // 如果位置为可填入位置
for (int x = 0; x < 9; x++) { // 检验同列中是否有已存在于set中的元素
isContain(sd[i][x]); // 若存在则删除set中的对应元素
}
for (int x = 0; x < 9; x++) { // 检验同行中是否有已存在于set中的元素
isContain(sd[x][j]); // 若存在则删除set中的对应元素
} if (i >= 0 && i <= 2 && j >= 0 && j <= 2) { // 判断sd[i][j]是否处于第一个九宫格内
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
isContain(sd[x][y]);
}
}
} if (i >= 0 && i <= 2 && j >= 3 && j <= 5) { // 判断sd[i][j]是否处于第二个九宫格内
for (int x = 0; x < 3; x++) {
for (int y = 3; y < 6; y++) {
isContain(sd[x][y]);
}
}
}
if (i >= 0 && i <= 2 && j >= 6 && j <= 8) { // 判断sd[i][j]是否处于第三个九宫格内
for (int x = 0; x < 3; x++) {
for (int y = 6; y < 9; y++) {
isContain(sd[x][y]);
}
}
} if (i >= 3 && i <= 5 && j >= 0 && j <= 2) { // 判断sd[i][j]是否处于第四个九宫格内
for (int x = 3; x < 6; x++) {
for (int y = 0; y < 3; y++) {
isContain(sd[x][y]);
}
}
}
if (i >= 3 && i <= 5 && j >= 3 && j <= 5) { // 判断sd[i][j]是否处于第五个九宫格内
for (int x = 3; x < 6; x++) {
for (int y = 3; y < 6; y++) {
isContain(sd[x][y]);
}
}
}
if (i >= 3 && i <= 5 && j >= 6 && j <= 8) { // 判断sd[i][j]是否处于第六个九宫格内
for (int x = 3; x < 6; x++) {
for (int y = 6; y < 9; y++) {
isContain(sd[x][y]);
}
}
} if (i >= 6 && i <= 8 && j >= 0 && j <= 2) { // 判断sd[i][j]是否处于第七个九宫格内
for (int x = 6; x < 9; x++) {
for (int y = 0; y < 3; y++) {
isContain(sd[x][y]);
}
}
}
if (i >= 6 && i <= 8 && j >= 3 && j <= 5) { // 判断sd[i][j]是否处于第八个九宫格内
for (int x = 6; x < 9; x++) {
for (int y = 3; y < 6; y++) {
isContain(sd[x][y]);
}
}
}
if (i >= 6 && i <= 8 && j >= 6 && j <= 8) { // 判断sd[i][j]是否处于第九个九宫格内
for (int x = 6; x < 9; x++) {
for (int y = 6; y < 9; y++) {
isContain(sd[x][y]);
}
}
}
if (has == 1) {
for (int x = 0; x < 9; x++) {
if (set[x] != 0) {
sd[i][j] = set[x];
wait--;
break;
}
}
}
}
}
}
}
} //输出这个数独
void display() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(" "+sd[i][j]+" ");
if(j%3==0)
System.out.print("|");
}
if(i%3==0)
System.out.println("---------------------------------------------------");
else
System.out.println();
}
}
}
大哥,随便写个test类就能测试了,不想用回溯做啊就是,搜索状态树什么NPC问题都解决
public class Solution {
private int sd[][] = {
{ 0, 0, 3, 8, 0, 0, 0, 5, 9 },
{ 0, 6, 0, 0, 0, 7, 0, 8, 3 },
{ 5, 0, 0, 0, 4, 0, 0, 0, 0 },
{ 0, 0, 4, 6, 0, 0, 0, 1, 2 },
{ 2, 0, 0, 0, 8, 0, 0, 0, 0 },
{ 0, 8, 0, 0, 0, 9, 0, 4, 5 },
{ 0, 0, 0, 0, 0, 0, 0, 9, 1 },
{ 6, 5, 0, 0, 9, 8, 2, 0, 0 },
{ 4, 3, 0, 0, 5, 1, 6, 0, 0 },
};
private int set[]=new int [9];
private int wait=0;
private int has=0;
private void count()
{
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (sd[i][j]==0)
wait++;
}
private void initialize()
{
for (int i = 0; i < 9; i++)
set[i]=i+1;
has=9;
}
private void Has()
{
for (int i = 0; i < 9; i++) {
if (set[i]==0) {
has--;
}
}
}
private void checkGroup(int n,int i,int j)
{
int x,y;
x=i; y=j;
while(x%3!=0) x--;
while(y%3!=0) y--;
for (int a = x; a < x+3; a++) {
for (int b = y; b < y+3; b++) {
for (int c = 0; c < 9; c++) {
if(set[c]==sd[a][b])
{
set[c]=0;
}
}
}
}
} private void checkRow(int n,int i,int j)
{
for (int a = 0; a < 9; a++) {
for (int c = 0; c < 9; c++) {
if (set[c]==sd[i][a]) {
set[c]=0;
}
}
}
} private void checkList(int n,int i,int j)
{
for (int b = 0; b < 9; b++) {
for (int c = 0; c < 9; c++) {
if (set[c]==sd[b][j]) {
set[c]=0;
}
}
}
} private int getSet()
{
int get=0;
for (int c = 0; c < 9; c++) {
if (set[c]!=0) {
get=set[c];
break;
}
}
return get;
}
private void solution()
{
count();
while(wait!=0)
{
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
initialize();
if(sd[i][j]==0)
{
checkGroup(sd[i][j], i, j);
checkRow(sd[i][j], i, j);
checkList(sd[i][j], i, j);
Has();
if (has==1) {
sd[i][j]=getSet();
wait--;
}
}
}
}
}
} public void display()
{
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
{
System.out.print(sd[i][j]+" ");
if (j==8) {
System.out.println();
}
if (j%3==0) {
System.out.print("");
}
}
} public void solve()
{
solution();
}
}
重做的代码,有兴趣的看看
大哥,随便写个test类就能测试了,不想用回溯做啊就是,搜索状态树什么NPC问题都解决
亲,回溯法不是我说的哦,我也不懂啥子回溯法,才学让别个帮你看看吧。我也不大懂还有,妹子一枚,不是大哥啦!
大哥,随便写个test类就能测试了,不想用回溯做啊就是,搜索状态树什么NPC问题都解决
亲,回溯法不是我说的哦,我也不懂啥子回溯法,才学让别个帮你看看吧。我也不大懂还有,妹子一枚,不是大哥啦!哦,加油,上面重新发的程序已经可以解一些简单的数独了,嘿嘿,有空再弄吧