A cool algorithm to check a Sudoku field?

前端 未结 25 752
清酒与你
清酒与你 2021-01-30 09:19

Does anyone know a simple algorithm to check if a Sudoku-Configuration is valid? The simplest algorithm I came up with is (for a board of size n) in Pseudocode

f         


        
25条回答
  •  走了就别回头了
    2021-01-30 09:49

    First, you would need to make a boolean, "correct". Then, make a for loop, as previously stated. The code for the loop and everything afterwards (in java) is as stated, where field is a 2D array with equal sides, col is another one with the same dimensions, and l is a 1D one:

    for(int i=0; i9||field[i][j]<1){
                checking=false;
                break;
            }
            else{
                col[field[i].length()-j][i]=field[i][j];
            }
        }
    }
    

    I don't know the exact algorithim to check the 3x3 boxes, but you should check all the rows in field and col with "/*array name goes here*/[i].contains(1)&&/*array name goes here*/[i].contains(2)" (continues until you reach the length of a row) inside another for loop.

提交回复
热议问题