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

A Sudoku game board cosists of a partially-filled in 9*9 grid of numbers from 1-

ID: 3540416 • Letter: A

Question

A Sudoku game board cosists of a partially-filled in 9*9 grid of numbers from 1-9. The objective of the game is to fill in the board using the numbers 1-9 such that:


a. Every row in the board contains each number from 1-9 exactly once.

b. Every column in the board contains each number from 1-9 exactly once.

c. Every 3*3 " subgrid "  of the board contains each number from 1-9 exactly once.


For example, the following picture shows a solved puzzle. The numbers in black are the ones that were originally filled in; the numbers with a slightly lighter shade of black are the ones placed by the player.


Write a method public static boolean isValidSolution(int [ ] [ ] a) that takes a 2-D array a as a parameter and returns whether or not it's a valid Sudoku solution. You can assume that the 2-D array will always be of the correct dimensions (9*9).


Note that I'm not asking you to generate the puzzle or draw anything on the screen - all the method needs to do is to return whether or not the solution represented by the parameter array is valid (i.e., satisfying all three conditions above).


5

3

4

6

7

8

9

1

2

6

7

2

1

9

5

3

4

8

1

9

8

3

4

2

5

6

7

8

5

9

7

6

1

4

2

3

4

2

6

8

5

3

7

9

1

7

1

3

9

2

4

8

5

6

9

6

1

5

3

7

2

8

4

2

8

7

4

1

9

6

3

5

3

4

5

2

8

6

1

7

9

  

5

     

3

     

4

     

6

     

7

     

8

     

9

     

1

     

2

     

6

     

7

     

2

     

1

     

9

     

5

     

3

     

4

     

8

     

1

     

9

     

8

     

3

     

4

     

2

     

5

     

6

     

7

     

8

     

5

     

9

     

7

     

6

     

1

     

4

     

2

     

3

     

4

     

2

     

6

     

8

     

5

     

3

     

7

     

9

     

1

     

7

     

1

     

3

     

9

     

2

     

4

     

8

     

5

     

6

     

9

     

6

     

1

     

5

     

3

     

7

     

2

     

8

     

4

     

2

     

8

     

7

     

4

     

1

     

9

     

6

     

3

     

5

     

3

     

4

     

5

     

2

     

8

     

6

     

1

     

7

     

9

  

Explanation / Answer

please rate - thanks


any questions ask

I've indicated the invalid spot



import java.util.*;
public class sudokuChecker
{public static void main(String[] args)
{Scanner in=new Scanner(System.in);
int[][]board={{5,3,4,6,7,8,9,1,2},
              {6,7,2,1,9,5,3,4,8},
                {1,9,8,3,4,2,5,6,7},
                {8,5,9,7,6,1,4,2,3},
                {4,2,6,8,5,3,7,9,1},
                {7,1,3,9,2,4,8,5,6},
                {9,6,1,5,3,7,2,8,4},
                {2,8,7,4,1,9,6,3,5},
                {3,4,5,2,8,6,1,7,9}};
printBoard(board);
if(isValidSolution(board))
    System.out.println("Valid Solution");
else
    System.out.println("Invalid Solution");

}
public static void printBoard(int b[][])
{int i,j;
for(i=0;i<9;i++)
   {for(j=0;j<9;j++)
       System.out.print(b[i][j]+" ");
    System.out.println();
    }

}
public static boolean isValidSolution(int [ ] [ ] a)
{int i,j;
boolean good=true;
for(i=0;i<9&&good;i++)
     good=good&&row(a,i);
if(!good)
    return false;
for(i=0;i<9&&good;i++)
     good=good&&col(a,i);
if(!good)
    return false;
for(i=0;i<7&&good;i++)
     for(j=0;j<7&&good;j++)
        good=good&&section(a,i,j);
if(!good)
    return false;
return true;
}        
public static boolean row(int a[][],int index)
{int []used = new int[9];
int i;
for(i=0;i<9;i++)
     used[i]=0;
for(i=0;i<9;i++)
    used[a[index][i]-1]++;   
for(i=0;i<9;i++)
    {if(used[i]!=1)
         return false;
        }
return true;
}
  
public static boolean col(int a[][],int index)
{int []used = new int[9];
int i;
for(i=0;i<9;i++)
     used[i]=0;
for(i=0;i<9;i++)
    used[a[i][index]-1]++;
for(i=0;i<9;i++)
    if(used[i]!=1)
         return false;
return true;
}
public static boolean section(int a[][],int row,int col)
{int []used = new int[9];
int i,j;
for(i=0;i<9;i++)
     used[i]=0;
for(i=row;i<row+3;i++)
    for(j=col;j<col+3;j++)
       used[a[i][j]-1]++;
for(i=0;i<9;i++)
   if(used[i]!=1)
        return false;
return true;
}

}

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