【问题标题】:Creating 9 subarrays from a 9x9 2d array java从 9x9 二维数组 java 创建 9 个子数组
【发布时间】:2012-03-10 13:46:29
【问题描述】:

您好,我需要帮助从 9x9 数组创建 9 个 3x3 维度的子数组。我已经看到 stackOverflow 已经提出了类似的问题,但不幸的是它是在 c++ 中。谁能指出我如何创建子数组的正确方向。

编辑:有一个相似的更改为有一个相似的

public static void Validate(final int[][] sudokuBoard)
{
    int width = sudokuBoard[0].length;
    int height = sudokuBoard.length;

    for(int i = 0; i < width; i++)

        if(!IsValidRow(sudokuBoard, i, width))
        {
            System.out.print("(Row)" + i + "  Error Detected \n");
          //Do something - The row has repetitions
        }
    for(int j = 0; j < height; j++)
        if(!IsValidColumn(sudokuBoard, j, height))
        {
            System.out.print(" (Column)" + j + "  Error Detected \n");
          //Do something - The columns has repetitions
        }
  // for(int i=0; i<3; i++)
    //  if(!isBlock1Valid(sudokuBoard,width, height)){
        //  System.out.print("hi");
        //}

}

static boolean isBlock1Valid(int[][] sudokuBoard, int referenceRow, int referenceColumn)
{
    block1
    boolean[] seen = new boolean[9];

    for (int i = 0; i < 3; i++){

        for (int j = 0; j < 3; j++){

            if ( seen[sudokuBoard[referenceColumn+i][referenceRow+j]]) return false;


    else ( seen[sudokuBoard[referenceColumn+i][referenceRow+j]]) = true;
    }
    }
return true;
}

这是我的验证类,它调用我已实现的布尔表达式。我不确定在验证中发送布尔值的参数。并考虑重新排列它,以便我发送 3x3 尺寸的块。

和 c++ 链接 Dividing a 9x9 2d array into 9 sub-grids (like in sudoku)? (C++)

【问题讨论】:

  • 可以分享C++题的链接吗?
  • 用我当前的代码更新了问题。并链接到 c++ 问题
  • 我想知道你是否意识到数组在内存中表示为连续块,所以它都是关于索引的。
  • 不,我不知道,但我确实有点理解只考虑索引的概念。
  • @Downvoter : 总是关心解释,哪里错了,哪里错了,至少让人们有机会通过分享你的智慧来改进。

标签: java sudoku arrays


【解决方案1】:

试试这个代码 sn-p,它可以显示给定块内的不同索引,通过使用 int block = (((row / 3) * 3) + (column / 3)); 获得

import java.util.*;

public class TwoDArray
{
    public static void main(String... args) throws Exception
    {
        Scanner scanner = new Scanner(System.in);        
        int[][] array = {    
                             {0, 1, 2, 3, 4, 5, 6, 7, 8},       
                             {9, 10, 11, 12, 13, 14, 15, 16, 17},                                                                                       
                             {18, 19, 20, 21, 22, 23, 24, 25, 26},
                             {27, 28, 29, 30, 31, 32, 33, 34, 35},
                             {36, 37, 38, 39, 40, 41, 42, 43, 44},
                             {45, 46, 47, 48, 49, 50, 51, 52, 53},
                             {54, 55, 56, 57, 58, 59, 60, 61, 62},
                             {63, 64, 65, 66, 67, 68, 69, 70, 71},
                             {72, 73, 74, 75, 76, 77, 78, 79, 80}
                            };  

        displayMatrix(array);
        displayEachBlock(array);        
    }

    private static void displayMatrix(int[][] array)
    {
        for (int i = 0; i < array.length; i++)
        {
            if (i == 3 || i == 6)
                System.out.println("------------------------------------");
            for (int j = 0; j < array[i].length; j++)
            {
                System.out.format("%-3s", array[i][j]);
                if (j == 2 || j == 5 || j == 8)
                    System.out.print(" | ");
            }           
            System.out.println();   
        }      
        System.out.println("------------------------------------");
    }

    private static void displayEachBlock(int[][] array)
    {
        for (int i = 0; i < array.length; i += 3)
        {           
            for (int j = 0; j < array[i].length; j += 3)
            {
                /*
                 * Here we are finding which block we are standing at.
                 */
                int block = (((i / 3) * 3) + (j / 3));
                System.out.println("Block : " + block);
                int[][] newArray = new int[3][3];
                int newRow = 0;
                for (int k = i; k < (i + 3); k++)
                {
                    int newColumn = 0;
                    for (int l = j; l < (j + 3); l++)
                    {
                        // This is where you are getting your array inside the given block.
                        newArray[newRow][newColumn] = array[k][l];
                        System.out.format("[%-1s][%-1s] : %-3s ", newRow, newColumn, newArray[newRow][newColumn++]);
                    }
                    newRow++;
                    System.out.println();
                }
                // Here you can send your newArray for VALIDATION, thingy.
                // So that we can move on to the next Block for further processing.
            }
        }
    }
}

这是这段代码 sn-p 的输出:

0  1  2   | 3  4  5   | 6  7  8   |
9  10 11  | 12 13 14  | 15 16 17  |
18 19 20  | 21 22 23  | 24 25 26  |
------------------------------------
27 28 29  | 30 31 32  | 33 34 35  |
36 37 38  | 39 40 41  | 42 43 44  |
45 46 47  | 48 49 50  | 51 52 53  |
------------------------------------
54 55 56  | 57 58 59  | 60 61 62  |
63 64 65  | 66 67 68  | 69 70 71  |
72 73 74  | 75 76 77  | 78 79 80  |
------------------------------------
Block : 0
[0][0] : 0   [0][1] : 1   [0][2] : 2
[1][0] : 9   [1][1] : 10  [1][2] : 11
[2][0] : 18  [2][1] : 19  [2][2] : 20
Block : 1
[0][0] : 3   [0][1] : 4   [0][2] : 5
[1][0] : 12  [1][1] : 13  [1][2] : 14
[2][0] : 21  [2][1] : 22  [2][2] : 23
Block : 2
[0][0] : 6   [0][1] : 7   [0][2] : 8
[1][0] : 15  [1][1] : 16  [1][2] : 17
[2][0] : 24  [2][1] : 25  [2][2] : 26
Block : 3
[0][0] : 27  [0][1] : 28  [0][2] : 29
[1][0] : 36  [1][1] : 37  [1][2] : 38
[2][0] : 45  [2][1] : 46  [2][2] : 47
Block : 4
[0][0] : 30  [0][1] : 31  [0][2] : 32
[1][0] : 39  [1][1] : 40  [1][2] : 41
[2][0] : 48  [2][1] : 49  [2][2] : 50
Block : 5
[0][0] : 33  [0][1] : 34  [0][2] : 35
[1][0] : 42  [1][1] : 43  [1][2] : 44
[2][0] : 51  [2][1] : 52  [2][2] : 53
Block : 6
[0][0] : 54  [0][1] : 55  [0][2] : 56
[1][0] : 63  [1][1] : 64  [1][2] : 65
[2][0] : 72  [2][1] : 73  [2][2] : 74
Block : 7
[0][0] : 57  [0][1] : 58  [0][2] : 59
[1][0] : 66  [1][1] : 67  [1][2] : 68
[2][0] : 75  [2][1] : 76  [2][2] : 77
Block : 8
[0][0] : 60  [0][1] : 61  [0][2] : 62
[1][0] : 69  [1][1] : 70  [1][2] : 71
[2][0] : 78  [2][1] : 79  [2][2] : 80

【讨论】:

  • 这看起来确实令人印象深刻。我的后续问题是我应该在哪里输入数组信息来填充新块?
  • @VOr : 试试我刚刚制作的这个新程序,如何获取块值 :-) 希望这会带来更多亮点
  • 确实如此。太感谢了。非常感谢您的所有帮助
  • @VOr:欢迎您。很高兴在这方面对您有所帮助。保持微笑:-)
  • 它工作得几乎完美无缺。我刚遇到 ArrayIndexOutOfBound:9。再次感谢:)
【解决方案2】:

您有一个 9x9 二维整数数组。您的目标是将其分成 9 个 3x3 2D 整数数组。看看check() 方法,它控制一个 3x3 数组以获取重复数字。 如果找到重复的数字,则返回 false;

检查和测试我的代码:

import java.util.ArrayList;

public class Main
{
    public static void main(String[] args)
    {
        int[][] input = {{1,1,1,1,1,1,1,1,1},
                         {2,2,2,2,2,2,2,2,2},
                         {3,3,3,3,3,3,3,3,3},
                         {4,4,4,4,4,4,4,4,4},
                         {1,2,3,4,5,6,7,8,9},
                         {6,6,6,6,6,6,6,6,6},
                         {7,7,7,7,7,7,7,7,7},
                         {8,8,8,8,8,8,8,8,8},
                         {9,9,9,9,9,9,9,9,9}};

        int[][][] output = get3DVersion(input);

        for(int i=1; i<=output.length; i++)
            System.out.println("Validity of subArray #"+i+" : " +check(output[i-1]));
    }

    public static boolean check(int[][] array)
    {
        ArrayList<Integer> soFar = new ArrayList<Integer>();

         for(int j=0; j<3; j++)
             for(int k=0; k<3; k++)
             {
                 if(soFar.contains(array[j][k]))
                     return false;
                 else
                     soFar.add(array[j][k]);
             }
         return true;
    }

    public static int[][][] get3DVersion(int[][] input)
    {
        int[][][] output = new int[9][3][3];

        for(int i=0; i<9; i++)
            for(int j=0; j<3; j++)
                for(int k=0; k<3; k++)
                    output[i][j][k] = input[i][j*3+k];

        output[8][2][2] = input[8][8];

        return output;
    }
}

【讨论】:

  • 我不明白为什么 3d 数组对我有帮助。感谢您的建议。非常感谢
  • 您提到了大约 9 个 3x3 阵列。这意味着一个 3D 阵列。我错了吗?
  • 我可能措辞不正确。我指的是 9 [3][3] 个数组。
  • 好的,使用这些代码,您已经获得了 9 [3][3] 个数组,它们是 get3DVersion() 方法中的“输出”。您可以单独获取和使用它们。试一试。很简单。例如 output[0] 指的是你的第一个 3X3 数组。
  • 我只是不确定如何创建一个布尔值来检查 3d 数组的所有整数,以确保 1-9 只出现一次。
猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2016-12-27
  • 2011-02-11
  • 2018-04-14
  • 2023-01-04
  • 2021-06-09
相关资源
最近更新 更多