java数独回溯递归
嘿,我已经编写了这个程序来解决数独问题,但它只适用于数独矩阵的几个单元格,其他单元格返回0。你能理解这有什么问题吗? 我是java编码新手,不能编写简单的程序真的很痛苦java数独回溯递归,java,recursion,sudoku,backtracking,Java,Recursion,Sudoku,Backtracking,嘿,我已经编写了这个程序来解决数独问题,但它只适用于数独矩阵的几个单元格,其他单元格返回0。你能理解这有什么问题吗? 我是java编码新手,不能编写简单的程序真的很痛苦 public class sudoku { static int sud[][] = new int[9][9]; public static void main(String args[]) { for (int i = 0; i < 9; i++) { for (int j = 0; j
public class sudoku {
static int sud[][] = new int[9][9];
public static void main(String args[]) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sud[i][j] = 0;
}
}
solve(sud);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(sud[i][j]);
}
System.out.print("\n");
}
}
public static boolean solve(int[][] sud) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (sud[i][j] != 0) {
continue;
}
for (int x = 1; x < 10; x++) {
if (!used(i, j, x)) {
sud[i][j] = x;
if (solve(sud)) {
return true;
}
}
}
return false;
}
}
return true;
}
public static boolean isinrow(int i, int j, int x) {
for (int t = 0; t < 9; t++) {
if (sud[i][t] == x) {
return true;
}
}
return false;
}
public static boolean isincol(int i, int j, int x) {
for (int t = 0; t < 9; t++) {
if (sud[t][j] == x) {
return true;
}
}
return false;
}
public static boolean isinsq(int sr, int sc, int x) {
for (sr = 0; sr < 3; sr++) {
for (sc = 0; sc < 3; sc++) {
if (sud[sr][sc] == x) {
return true;
}
}
}
return false;
}
static boolean used(int i, int j, int x) {
if (!isinrow(i, j, x)) {
if (!isincol(i, j, x)) {
if (!isinsq(i - (i % 3), j - (j % 3), x)) {
return false;
}
}
}
return true;
}
公共级数独{
静态int sud[][]=新int[9][9];
公共静态void main(字符串参数[]){
对于(int i=0;i<9;i++){
对于(int j=0;j<9;j++){
sud[i][j]=0;
}
}
解决(sud);
对于(int i=0;i<9;i++){
对于(int j=0;j<9;j++){
系统输出打印(sud[i][j]);
}
系统输出打印(“\n”);
}
}
公共静态布尔解算(int[][]sud){
对于(int i=0;i<9;i++){
对于(int j=0;j<9;j++){
如果(sud[i][j]!=0){
继续;
}
对于(int x=1;x<10;x++){
如果(!使用(i,j,x)){
sud[i][j]=x;
if(求解(sud)){
返回true;
}
}
}
返回false;
}
}
返回true;
}
公共静态布尔值isinrow(int i,int j,int x){
for(int t=0;t<9;t++){
if(sud[i][t]==x){
返回true;
}
}
返回false;
}
公共静态布尔isincol(int i,int j,int x){
for(int t=0;t<9;t++){
if(sud[t][j]==x){
返回true;
}
}
返回false;
}
公共静态布尔isinsq(int-sr、int-sc、int-x){
对于(sr=0;sr<3;sr++){
对于(sc=0;sc<3;sc++){
if(sud[sr][sc]==x){
返回true;
}
}
}
返回false;
}
使用静态布尔值(int i、int j、int x){
如果(!isinrow(i,j,x)){
if(!isincol(i,j,x)){
如果(!isinsq(i-(i%3),j-(j%3),x)){
返回false;
}
}
}
返回true;
}
}在我看来,
isInSq
总是要检查左上角的方块,因为当你进入循环时,你会删除你的参数。您应该从sr
转到sr+2
,对于sc
也是如此。我不认为这是唯一的问题,但它看起来是一个好的开始
作为另一个提示,我将修复您的变量名。你也许能理解它们,但这会让其他人更难阅读。在我看来,
isInSq
总是要检查左上角的方块,因为你进入循环时会删除你的参数。您应该从sr
转到sr+2
,对于sc
也是如此。我不认为这是唯一的问题,但它看起来是一个好的开始
作为另一个提示,我将修复您的变量名。您可能能够理解它们,但这会使其他人更难阅读。您的问题在于您正在执行的功能
public static boolean isinsq(int sr, int sc, int x) {
for ( sr = 0; sr < 3; sr++) {
// ^ you are reseting the value of sr that you pass in
// effectivel making it so that you always check the first square
for ( sc = 0; sc < 3; sc++) {
// ^ same here
if (sud[sr][sc] == x) {
return true;
}
}
}
return false;
}
publicstaticbooleaninsq(intsr,intsc,intx){
对于(sr=0;sr<3;sr++){
//^您正在重置传入的sr值
//使其有效,以便始终检查第一个正方形
对于(sc=0;sc<3;sc++){
//^这里也一样
if(sud[sr][sc]==x){
返回true;
}
}
}
返回false;
}
这是将sr重置回0,因此您只检查了左上角,而不是您要传递的坐标。你应该这样做:
public static boolean isinsq(int xcorner, int ycorner, int x) {
for (int sr = xcorner; sr < 3; sr++) {
//^ here w create a new variable with the starting value that you passed in
for ( int sc = ycorner; sc < 3; sc++) {
//^ here w create a new variable with the starting value that you passed in
if (sud[sr][sc] == x) {
return true;
}
}
}
return false;
}
publicstaticbooleaninsq(intxcorner,intycorner,intx){
对于(int-sr=xcorner;sr<3;sr++){
//^在这里,我们用传入的起始值创建一个新变量
对于(int-sc=ycorner;sc<3;sc++){
//^在这里,我们用传入的起始值创建一个新变量
if(sud[sr][sc]==x){
返回true;
}
}
}
返回false;
}
作为辅助工具,在solve中的双嵌套for循环是不必要的,并且会增加大量开销。与其寻找下一个开放点,为什么不假设它们都是开放的,并检查它们是否不是这样,您的递归将为您处理迭代。考虑这样的事情(它也更简单…至少对我来说)
bool solve(int[]sud,int x,int y)
{
//这里需要确保当x>9时,我们将x设置为0并增加y
//另外,如果x==9和y==9,则需要处理结束条件
如果(sud[x][y]==0)//不是预先给定的值,那么我们可以更改它
{
对于(inti=1;i您的问题在于您所做的这个函数
public static boolean isinsq(int sr, int sc, int x) {
for ( sr = 0; sr < 3; sr++) {
// ^ you are reseting the value of sr that you pass in
// effectivel making it so that you always check the first square
for ( sc = 0; sc < 3; sc++) {
// ^ same here
if (sud[sr][sc] == x) {
return true;
}
}
}
return false;
}
publicstaticbooleaninsq(intsr,intsc,intx){
对于(sr=0;sr<3;sr++){
//^您正在重置传入的sr值
//使其有效,以便始终检查第一个正方形
对于(sc=0;sc<3;sc++){
//^这里也一样
if(sud[sr][sc]==x){
返回true;
}
}
}
返回false;
}
这是将sr重置回0,因此您只检查了左上角,而不是您要传递的坐标。您应该执行以下操作:
public static boolean isinsq(int xcorner, int ycorner, int x) {
for (int sr = xcorner; sr < 3; sr++) {
//^ here w create a new variable with the starting value that you passed in
for ( int sc = ycorner; sc < 3; sc++) {
//^ here w create a new variable with the starting value that you passed in
if (sud[sr][sc] == x) {
return true;
}
}
}
return false;
}
publicstaticbooleaninsq(intxcorner,intycorner,intx){
对于(int-sr=xcorner;sr<3;sr++){
//^在这里,我们用传入的起始值创建一个新变量
对于(int-sc=ycorner;sc<3;sc++){
//^在这里,我们用传入的起始值创建一个新变量
if(sud[sr][sc]==x){
返回true;
}
}
}
返回false;
}
作为辅助工具,在solve中使用双嵌套for循环是不必要的,并且会增加大量开销