sudoku me_Java Sudoku解算器程序

本文介绍如何使用Java编写数独解算器。通过理解数独的逻辑约束,如行、列和小宫格的独特性,利用回溯算法解决数独谜题。文章详细解释了辅助函数的实现,包括获取未分配位置、检查数字可用性,以及完整的数独解决方法。最终,提供的Java代码能在几秒钟内解决数独问题,超越人类最快纪录。
摘要由CSDN通过智能技术生成

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
Sudoku Representation
数独表示法

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可以帮助我们确定用户要填写的位置。

用Java解决数独 (

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值