sudoku me
Remember Sudoku sections from newspapers? That grid-like puzzle filled with numbers.
还记得报纸上的数独版吗? 网格状的难题充满了数字。
Sudoku is a logic-based, combinatorial number-placement puzzle. In classic sudoku, the objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid contain all of the digits from 1 to 9.
数独是一个基于逻辑的组合数字放置难题。 在经典的数独中,目标是用数字填充9×9网格,以便组成该网格的每一列,每一行以及9个3×3子网格中的每一个都包含从1到9的所有数字。
To solve the sudoku, you need to fill in the digits such that your solution violates none of the constraints. It can be harder than it sounds…..not for a computer though.
要解决数独问题,您需要填写数字,以使您的解决方案不受任何约束。 它可能比听起来难……虽然不是计算机。
In this article, we will learn how to write Java code that solves sudoku within seconds.
在本文中,我们将学习如何编写可在数秒内解决数独问题的Java代码。
The world record for solving a Sudoku is 1 minute 23.93 seconds. Let’s break that. Shall we?
解决数独的世界纪录是1分23.93秒。 让我们打破这一点。 我们可以?
数独数约束 (Sudoku Number Constraints)
What are the constraints in solving a sudoku?
解决数独有哪些限制条件?
- A number can occur only once in a row 一个数字只能连续出现一次
- A number can occur only once in a column 一列中只能出现一次数字
- A number can occur only once in a subgrid. 一个数字只能在一个子网格中出现一次。
While solving, we will use these constraints to design our helper functions.
在求解时,我们将使用这些约束来设计我们的辅助函数。
Let’s get started!
让我们开始吧!
数独的表示 (Representation of a Sudoku )
A sudoku problem comes with some digits filled in already. The goal is to figure out the missing digits through trial and error.
数独问题附带一些数字已填充。 目的是通过反复试验找出丢失的数字。
The sudoku will be a 2D array for the computer.
数独将是计算机的2D阵列。
In our representation we will represent the missing digits with 0.
在我们的表示中,我们将用0表示缺失的数字。
![Sudoku](https://i-blog.csdnimg.cn/blog_migrate/77999ae52d40691bb7249d42333cdadc.png)
Representation of the sudoku above is:
上面的数独表示为:
int[][] arr = {
{5, 8, 0, 2, 0, 0, 4, 7, 0},
{0, 2, 0, 0, 0, 0, 0, 3, 0},
{0, 3, 0, 0, 5, 4, 0, 0, 0},
{0, 0, 0, 5, 6, 0, 0, 0, 0},
{0, 0, 7, 0, 3, 0, 9, 0, 0},
{0, 0, 0, 0, 9, 1, 0, 0, 0},
{0, 0, 0, 8, 2, 0, 0, 6, 0},
{0, 7, 0, 0, 0, 0, 0, 8, 0},
{0, 9, 4, 0, 0, 6, 0, 1, 5}};
The digit 0 helps us in identifying which place is for the user to fill.
数字0可以帮助我们确定用户要填写的位置。