Java Sudoko网格从9*9转换为所有网格大小
我已经用Java编写了数独游戏的代码。问题是我的代码对9*9网格的输入有限制。如何使代码适用于所有网格。请耐心等待。我是java新手 我需要做哪些更改,以便代码可以在所有网格大小上运行?网格是正方形而不是矩形Java Sudoko网格从9*9转换为所有网格大小,java,arrays,arraylist,Java,Arrays,Arraylist,我已经用Java编写了数独游戏的代码。问题是我的代码对9*9网格的输入有限制。如何使代码适用于所有网格。请耐心等待。我是java新手 我需要做哪些更改,以便代码可以在所有网格大小上运行?网格是正方形而不是矩形 class Solution { public void solveSudoku(char[][] board) { if(solveSudoku2(board)) { return; } } public
class Solution {
public void solveSudoku(char[][] board) {
if(solveSudoku2(board)) {
return;
}
}
public boolean solveSudoku2(char[][] board) {
boolean isEmpty = true;
int row = -1;
int col = -1;
int n = board.length;
//this code is used to check if there exists any empty cell in sudoku board
//if there is any empty cell, that means we are not done yet and we need to solve it further,
// so we cannot return true at any point until all the cells are full
//by empty cell, I mean cells having '.' as the value
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(board[i][j] == '.') {
row = i;
col = j;
isEmpty = false;
break;
}
}
if(!isEmpty) {
break;
}
}
if(isEmpty) {
return true;
}
//loop for all the numbers and start placing in the empty cells
//numbers start from 1 to n
for(int num = 1; num <= n; num++) {
//convert number to char
char char_num = (char)(num + '0');
//check if the number we are adding satisfies all the sudoku rules,
// if it does, then we place that number in the cell
if(checkSafe(board,char_num,row,col)) {
board[row][col] = (char)(num + '0');
//using this number in place row,col, we check for all the other empty places and see if the board is returning true or not
// if the board is not filled that means that we need to use other number in row,col place.
//hence backtrack.
if(solveSudoku2(board)) {
return true;
} else {
board[row][col] = '.';
}
}
}
return false;
}
public boolean checkSafe(char[][] board, char num, int row, int col) {
//checkk if num is present in the row
for(int i = 0; i< board.length; i++ ) {
if(board[row][i] == num) {
return false;
}
}
for(int j = 0; j < board[0].length; j++) {
if(board[j][col] == num) {
return false;
}
}
int checknum = (int)Math.sqrt(board.length);
//check for the current grid. grid will be basically checknum*checknum matrix. where every matrix will start from startrow to startrow + checknum having checknum length.
// so, we we have row = 0, then matrix will start from 0 to 2, i.e. the first 3x3 matrix.
// however, we have row = 2, then also the matrix will start from 0 to 2 - the first 3x3 matrix.
//however, if row = 3, then we will start our matrix from 3 and cotinute upto 5.
int startrow = row - row % checknum;
int startcol = col - col % checknum;
for(int k = startrow; k < startrow + checknum; k++) {
for(int l = startcol; l < startcol + checknum; l++) {
if(board[k][l] == num) {
return false;
}
}
}
return true;
}
}
类解决方案{
公共数独(char[][]板){
国际单项体育联合会(第二委员会){
返回;
}
}
公共布尔解算器SUDOKU2(字符[][]板){
布尔值isEmpty=true;
int行=-1;
int col=-1;
int n=电路板长度;
//此代码用于检查数独板中是否存在空单元格
//如果有空的单元,那意味着我们还没有完成,我们需要进一步解决它,
//因此,在所有单元格都满之前,我们无法在任何时候返回true
//空单元格是指以“.”作为值的单元格
对于(int i=0;i 对于(int num=1;num您的算法通过查找第一个空单元格,将第一个可用的数字放入该单元格,然后在尝试下一个数字之前,递归地尝试求解电路板的其余部分,假设该数字正确。您就是这样玩数独的吗?:-)除此之外。你在9x9板上使用了字符“1”到“9”。你想用什么作为10x10板的第10个值?10x10板可能吗?它可能必须是3的倍数,对吗?实际上,不是3的倍数。它必须是一个完美的正方形数字。所以9、16、25、36等等,但不是10或12。可以使用NxNN个符号很容易…11,12,13…但是数据类型可能需要重新考虑。