Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sudoku is a number placement puzzle: a 9 x 9 grid made up of 3 x 3 subgrids. Som

ID: 663313 • Letter: S

Question

Sudoku is a number placement puzzle: a 9 x 9 grid made up of 3 x 3 subgrids. Some cells already contain numbers, known as givens, for example: The goal is to fill in the empty cells, one number in each, so that each column, row, and subgrid contains the numbers 1 - 9 exactly once. A player often needs to determine which numbers can be placed in a particular cell location. Given a partially filled game grid with 0s for empty cells, write a method public boolean[ ] findPossible (int [ ] [ ] grid, int i, int that returns a 9-element boolean array indicating whether each number is possible in the cell ith across and jth down from the top left corner (0,0). For example, if 1 is possible at the specified location, the first element of the boolean array should be true, otherwise false. Factor out the subtasks into separate methods to make your code easier to understand.

Explanation / Answer

public boolean[] findPossible(int[][] grid, int i, int j)
   {
       boolean[] result = new boolean[9];
       for(int m=0 ;m<9; m++)
           result[m] = true;
      
       for(int m=((i/3)*3); m<((i/3)*3)+3; m++) //find the sub grid starting x point
       {
           for(int n=((j/3)*3); n<((j/3)*3)+3; n++)//find the sub grid starting y point
           {
               if(grid[m][n] > 0) //check if the value is present
               {
                   result[grid[m][n]-1] = false; //if value present set its boolean value to false
               }
           }
       }
      
       for(int m=0; m<9; m++) //now the whole row and column
       {
           if(grid[i][m] > 0)
               result[grid[i][m]-1] = false; //sets if any data in the same column is present
          
           if(grid[m][j] > 0)
               result[grid[m][j]-1] = false;//sets if any data in the same row is present
       }
      
       return result; //return the data
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote