从9x9的二维数组使用Java创建子阵列9 [英] Creating 9 subarrays from a 9x9 2d array java

查看:698
本文介绍了从9x9的二维数组使用Java创建子阵列9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要帮助,从9x9的阵列创建的3x3尺寸的9个子阵列。我见过的StackOverflow有一个类似的问题已经问过,但不幸的是它是用C ++。任何人都可以点我如何创建一个子阵列的正确方向。

Hello I need help creating 9 sub arrays of 3x3 dimensions from a 9x9 array. I've seen that stackOverflow had a similar question already asked but unfortunately it was in c++. Can anyone point me in the right direction of how to create a a sub array.

编辑:曾AA类似改变也有类似

had aa similar changed to had a similar

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;
}

这是我的验证类调用我已经实现了布尔前​​pression。我不能确定的参数发送布尔的验证。心里想着重新安排它,这样我送的3x3尺寸的块来代替。

this is my validate class that calls the boolean expression that i have implemented. I'm unsure the parameters to send the boolean in Validate. and thought about rearranging it so that i send Blocks of 3x3 dimensions instead.

和C ++的链接
<一href=\"http://stackoverflow.com/questions/4718213/dividing-a-9x9-2d-array-into-9-sub-grids-like-in-sudoku-c\">Dividing 9x9的二维数组为9个子网格(如在数独)? (C ++)

推荐答案

试试这个code片段,它可以告诉你一个给定的块中不同的指数,通过使用INT 块=(获得((行/ 3)* 3)+(列/ 3));

Try this code snippet, it can show you different indices inside a given block, as obtained by using 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.
            }
        }
    }
}

在这里,这是code段的输出:

And here is the output of this code snippet :

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

这篇关于从9x9的二维数组使用Java创建子阵列9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆