class SudokuMatrix { private int[][] matrix = new int[][] { {0, 5, 0, 6, 0, 1, 0, 0, 0}, {0, 0, 7, 0, 9, 0, 0, 2, 0}, {3, 0, 0, 0, 0, 4, 0, 0, 1}, {9, 0, 0, 0, 0, 0, 1, 3, 0}, {0, 0, 8, 0, 5, 0, 7, 0, 0}, {0, 2, 6, 0, 0, 0, 0, 0, 8}, {7, 0, 0, 9, 0, 0, 0, 0, 5}, {0, 9, 0, 0, 4, 0, 3, 0, 0}, {0, 0, 0, 2, 0, 8, 0, 1, 0}, }; public void run(){ calc(); } private void calc() { int[] coordinate = getNextData(); int m = coordinate[0]; int n = coordinate[1]; for (int k = 1; k <= 9; k++) { if(checkNumber(m,n,k)) { matrix[m][n] = k; //System.out.println( m + "," + n + " with " + k ); if ( getNextData() == null ) { print(matrix); } else { calc(); } } } matrix[m][n] = 0; } private int[] getNextData(){ for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { if (matrix[row][col] == 0) { int[] coordinateOfEmptySquare = {row, col}; return coordinateOfEmptySquare; } } } return null; } private boolean checkNumber(int row, int col, int n) { for (int i = 0; i < 9; i++) { if (matrix[row][i] == n || matrix[i][col] == n) { return false; } } int[] leftTop = {row - (row % 3), col - (col % 3)}; for (int r = leftTop[0]; r <= leftTop[0] + 2; r++) { for (int c = leftTop[1]; c <= leftTop[1] + 2; c++) { if (matrix[r][c] == n) { return false; } } } return true; } public void print(int[][] grid) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { System.out.print(grid[i][j] + " "); } System.out.println(); } System.out.println(); } }
以上是数独小算法的实例详解的详细内容。更多信息请关注PHP中文网其他相关文章!
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
3 周前ByDDD
Windows 11 KB5054979中的新功能以及如何解决更新问题
2 周前ByDDD
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用