java - Sudoko 网格从 9*9 到所有网格大小的转换
问题描述
我用 Java 编写了数独谜题。问题是我的代码在为 9*9 网格提供输入方面存在限制。如何使我的代码适用于所有网格。请耐心等待。我是java新手。
我需要进行哪些更改才能使代码可以在所有网格尺寸上运行?网格是正方形而不是矩形。
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;
}
}
解决方案
推荐阅读
- c# - 无法在 Visual Studio 中添加 SecuBSMPx.dll 库?
- kubernetes - 跟踪失败的 Kubernetes 作业日志
- java - 通过spring xml beans在父类中设置子类中不可用的属性
- python - 如何单击元素并从链接的 xml 文件(python)中解析文本?
- ios - 支持在 IOS 应用中将 SAML 登录会话共享给不同的供应商
- permissions - 如何将斜杠命令隐藏到特定用户或频道 Discord.js v13
- django - 如何使用 xhtml2pdf Django 将 html 内容拆分为 2 页
- android - Android 开发者指南的问题开始另一个活动?
- python - Python - 坐标绘图
- angular - 在规范文件中将“and”函数链接到“selectAll”会引发错误