用java检测数独,小白求排bug,一个检验合法数独小程序

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

import java.util.*;

public class CheckSudokuSolution

{

public static void main(String[] args)

{

int[][] usersSolution = new int[9][9];

inputUsersSolution(usersSolution);

System.out.println(isSolutionValid(usersSolution) ? "Valid solution" : "Invalid solution");

}

public static void inputUsersSolution(int[][] list)

{

Scanner input = new Scanner(System.in);

for(int x = 0; x < list.length; x++)

{

for(int y = 0; y < list[x].length; y++)

{

list[x][y] = input.nextInt();

}

}

}

public static boolean isSolutionValid(int[][] list)

{

boolean isValid = true;

int[] temp = new int[9];

/*

for(int x = 0; x < 9; x++)

{

for(int y = 0; y < 9; y++)

{

temp[y] = list[x][y];

}

isValid = isOneDValid(temp);

if(!isValid)

break;

}

*/

/*

if(isValid)

{

for(int x = 0; x < 9; x++)

{

for(int y = 0; y < 9; y++)

{

temp[y] = list[y][x];

}

isValid = isOneDValid(temp);

if(!isValid)

break;

}

}

*/

if(isValid)

{

out:for(int x = 0; x < 2; x++)

{

for(int y = 0; y < 2; y++)

{

for(int z = 0; z < 3; z++)

{

for(int i = 0; i < 3; i++)

{

temp[z * 3 + i] = list[x * 3 + z][y * 3 + i];

}

}

isValid = isOneDValid(temp);

if(!isValid)

break out;

}

}

}

return isValid;

}

public static boolean isOneDValid(int[] list)

{

Arrays.sort(list);

for(int x = 0; x < 9; x++)

{

if(list[x] != x + 1)

return false;

}

return true;

}

}

就是最后验3*3那里出错了;不知道为什么

附上:

一个正确的数独:

9 6 3 1 7 4 2 5 8

1 7 8 3 2 5 6 4 9

2 5 4 6 8 9 7 3 1

8 2 1 4 3 7 5 9 6

4 9 6 8 5 2 3 1 7

7 3 5 9 6 1 8 2 4

5 8 9 7 1 3 4 6 2

3 1 7 2 4 6 9 8 5

6 4 2 5 9 8 1 7 3

检验3*3用的数独:

9 6 3 1 7 4 2 5 8

1 7 8 3 2 5 6 4 9

2 5 4 6 8 9 7 9 1

8 2 1 4 3 7 5 3 6

4 9 6 8 5 2 3 1 7

7 3 5 9 6 1 8 2 4

5 8 9 7 1 3 4 6 2

3 1 7 2 4 6 9 8 5

6 4 2 5 9 8 1 7 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值