如何使用for循环浏览每个子网格3x3?

发布于 2025-01-21 21:53:19 字数 531 浏览 0 评论 0原文

我一直在做Sudoku。原始网格的大小为9x9(包含9个列表的列表,每个列表是一行)。我需要检查数字是否仅出现每3x3子网格一次。为了做到这一点,我必须使用每个子网格使用循环(我认为)。因此,我花了很多时间尝试这样做,但是我似乎不明白它如何使用循环。

example_of_full_grid = [[5, 3, 4, 6, 7, 8, 9, 1, 2], 
           [6, 7, 2, 1, 9, 0, 3, 4, 9],
           [1, 0, 0, 3, 4, 2, 5, 6, 0],
           [8, 5, 9, 7, 6, 1, 0, 2, 0],
           [4, 2, 6, 8, 5, 3, 7, 9, 1],
           [7, 1, 3, 9, 2, 4, 8, 5, 6],
           [9, 0, 1, 5, 3, 7, 2, 1, 4],
           [2, 8, 7, 4, 1, 9, 6, 3, 5],
           [3, 0, 0, 4, 8, 1, 1, 7, 9]]

I have been working on sudoku. The size of the original grid is 9x9 (a list containing 9 lists, each of which is a row). I need to check whether the digits only occur once per 3x3 sub-grid. In order to do that I have to go through each sub-grid using for loop (I think). So, I spent quite some time trying to do that, but I cannot seem to understand how exactly do it using for loop.

example_of_full_grid = [[5, 3, 4, 6, 7, 8, 9, 1, 2], 
           [6, 7, 2, 1, 9, 0, 3, 4, 9],
           [1, 0, 0, 3, 4, 2, 5, 6, 0],
           [8, 5, 9, 7, 6, 1, 0, 2, 0],
           [4, 2, 6, 8, 5, 3, 7, 9, 1],
           [7, 1, 3, 9, 2, 4, 8, 5, 6],
           [9, 0, 1, 5, 3, 7, 2, 1, 4],
           [2, 8, 7, 4, 1, 9, 6, 3, 5],
           [3, 0, 0, 4, 8, 1, 1, 7, 9]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2025-01-28 21:53:19

是否可以为您使用numpy

所有9个子网格的代码循环。

import numpy as np

grid = np.array([[5, 3, 4, 6, 7, 8, 9, 1, 2], 
           [6, 7, 2, 1, 9, 0, 3, 4, 9],
           [1, 0, 0, 3, 4, 2, 5, 6, 0],
           [8, 5, 9, 7, 6, 1, 0, 2, 0],
           [4, 2, 6, 8, 5, 3, 7, 9, 1],
           [7, 1, 3, 9, 2, 4, 8, 5, 6],
           [9, 0, 1, 5, 3, 7, 2, 1, 4],
           [2, 8, 7, 4, 1, 9, 6, 3, 5],
           [3, 0, 0, 4, 8, 1, 1, 7, 9]])

for i in range(0,9,3):
    for j in range(0,9,3):
        print(grid[i:i+3,j:j+3])

必须更改列表。见下文:

subgrid = []
for i in range(0,9,3):
    row_3x3 = []
    for j in range(0,9):
        row_3x3.append(example_of_full_grid[j][i:i+3])
    for j in range(0,9,3):
        subgrid.append(row_3x3[j:j+3])
        print(row_3x3[j:j+3])

Is it possible to use numpy for you?

The code below loops over all 9 subgrids.

import numpy as np

grid = np.array([[5, 3, 4, 6, 7, 8, 9, 1, 2], 
           [6, 7, 2, 1, 9, 0, 3, 4, 9],
           [1, 0, 0, 3, 4, 2, 5, 6, 0],
           [8, 5, 9, 7, 6, 1, 0, 2, 0],
           [4, 2, 6, 8, 5, 3, 7, 9, 1],
           [7, 1, 3, 9, 2, 4, 8, 5, 6],
           [9, 0, 1, 5, 3, 7, 2, 1, 4],
           [2, 8, 7, 4, 1, 9, 6, 3, 5],
           [3, 0, 0, 4, 8, 1, 1, 7, 9]])

for i in range(0,9,3):
    for j in range(0,9,3):
        print(grid[i:i+3,j:j+3])

This has to be changed for a list. See below:

subgrid = []
for i in range(0,9,3):
    row_3x3 = []
    for j in range(0,9):
        row_3x3.append(example_of_full_grid[j][i:i+3])
    for j in range(0,9,3):
        subgrid.append(row_3x3[j:j+3])
        print(row_3x3[j:j+3])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文