数独 - 如何使用HashSet或Set?

时间:2016-12-23 18:30:42

标签: java set hashset

我正在尝试编写一个方法来检查我的数独板上的重复项。目前,我的方法getFrontier()总是返回true,我开始了解它是因为它只检查一个值而不是数组或值。我在squareCheck(),rowCheck()和columnCheck()中使用该方法3次。有没有办法对方法进行编码,以便保留输入的先前值,然后根据新值进行检查?

我目前的代码:

  public class validCheck {

public boolean isSolved(int[][][] board)
{
    for(int index = 0; index < board.length;index++)
    {
        for(int r = 0; r < board[0].length; r++)
        {
            for(int c = 0; c < board[0].length;c++)
            {
                if(board[index][r][c] == 0)
                  return false;
            }
        }
    }
    return true;
} 


 public boolean getFrontier(int value)
{
 Set<Integer> reserve = new HashSet<>();

 for(int n = 1; n < 10; n++)
 {
     if(value == n && reserve.contains(n))
         return false;

     else if(value == n) reserve.add(n);     
 }
return true;
}

public boolean squareCheck(int[][][] board, int index)
{
    for(int r = 0; r < board[0].length; r++)
    {
        for(int c = 0; c < board[0].length; c++)
        {
            if(!getFrontier(board[index][r][c]))
            {
                System.out.println("Square error at ["+index + r + c +"]");
                return false;
            }
        }
    }

    return true;

}

 public boolean isValid(int[][][] board)
 {     
     if(isSolved(board))
     {
        for(int i = 0; i < board.length; i++)
        {
            for(int r = 0; r < board[0].length;r++)
            {
                for(int c = 0; c < board[0].length;c++)
                {
                    if(!rowCheck(board,i,r) || !columnCheck(board,i,c) || !squareCheck(board,i))
                    {
                        return false;
                    }
                }
            }
        }
     }

     return true;

 }

 public boolean columnCheck(int[][][] board, int index, int col)
 {
     int target = 0;     

     if(index <=2)
     {
         target = index + 6;
     }
     else if(index > 2 && index < 6)
    {
        target = index +3;
        index = index - 3;
    }
     else if (index > 5)
     {
         target = index;
         index = index - 6;
     }

     while(index <= target)
     {
         for(int r = 0; r < board[0].length;r++)
         {

            if(!getFrontier(board[index][r][col]))
            {
                System.out.println("Column error at " + index + r + col);
                return false;    
            } 

         }
         index = index + 3;
     }
     return true;


 }


 public boolean rowCheck(int[][][] board, int index, int row)
 {
     int target = 0;

     if(index <= 2)
     {
         index = 0;
         target = 2;
     }
     else if (index <= 5)
     {
         index = 3;
         target  = 5;
     }
     else if(index <= 8)
     {
         index = 6;
         target = 8;
     }

     while(index <= target)
     {
            for(int c = 0; c < board[0].length; c++)
            {
                   if(!getFrontier(board[index][row][c]))
                   {
                       System.out.println("Row error at "+index+row+c);
                       return false;
                   }
             }
             index++;
      }

      return true;

     }


 }

用法:

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


validCheck checker = new validCheck();

       if(checker.isValid(solved))
            System.out.println(true);

       else System.out.println(false);
}

任何帮助都将非常感谢!!!

1 个答案:

答案 0 :(得分:0)

以下是我在2D数独板中找到有效的电路板配置的方法。我会使用一个HashSet作为行,另一个作为列,只要我们从不遇到重复,并且值包含1到我们知道该板有效的数组的长度。

    int [][] board = {{1,2,3}, 
                      {2,3,1}, 
                      {3,1,2}
                      };
    HashSet<Integer> rowDuplicates = new HashSet<>();
    HashSet<Integer> colDuplicates = new HashSet<>();
    boolean invalidBoard = false;

    for(int i = 0 ; i < board.length; i++)
    {
        for(int j = 0; j < board[i].length; j++)
        {
            if(rowDuplicates.contains(board[i][j]) || colDuplicates.contains(board[j][i]))
            {
                //this board is not valid
                invalidBoard = true;
            }
            else
            {
                rowDuplicates.add(board[i][j]);
                colDuplicates.add(board[j][i]);
            }
        }

        //now check they contain the correct numbers from 1 to the size of the array
        if(colDuplicates.size() == rowDuplicates.size())
        {
            for(int index = 0; index < colDuplicates.size(); index++)
            {
                if(!(colDuplicates.contains(index + 1) && rowDuplicates.contains(index + 1)))
                {
                    invalidBoard = true;
                    break;
                }
            }
        }
        else
        {
            invalidBoard = true;    
        }
        colDuplicates.clear();
        rowDuplicates.clear();

    }

    System.out.println("invalid board: " + invalidBoard);

您应该能够将其扩展到3D阵列,但是您可以看到使用HashSets验证有效的2D阵列数独板更容易。