Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how do I make this a typable sudoku like the ones you find on line because right now its displaying the grid not in full

how do I make this a typable sudoku like the ones you find on line because right now its displaying the grid not in full image with solution
public class SudokuPuzzle{
public boolean isSafe(int[][] b, int r, int c, int n)
{
for (int d =0; d < b.length; d++)
{
if (b[r][d]== n)
{
return false;
}
}
for (int r1=0; r1< b.length; r1++)
{
if (b[r1][c]== n)
{
return false;
}
}
int sqt =(int)Math.sqrt(b.length);
int boxRowSt = r - r % sqt;
int boxColSt = c - c % sqt;
for (int r1= boxRowSt; r1< boxRowSt + sqt; r1++)
{
for (int d = boxColSt; d < boxColSt + sqt; d++)
{
if (b[r1][d]== n)
{
return false;
}
}
}
return true;
}
public boolean solveSudoku(int[][] b, int num)
{
int r =-1;
int c =-1;
boolean isVacant = true;
for (int i =0; i < num; i++)
{
for (int j =0; j < num; j++)
{
if (b[i][j]==0)
{
r = i;
c = j;
isVacant = false;
break;
}
}
if (!isVacant)
{
break;
}
}
if (isVacant)
{
return true;
}
for (int no =1; no <= num; no++)
{
if (isSafe(b, r, c, no))
{
b[r][c]= no;
if (solveSudoku(b, num))
{
return true;
}
else
{
b[r][c]=0;
}
}
}
return false;
}
public void display(int[][] b, int n)
{
for (int i =0; i < n; i++)
{
for (int d =0; d < n; d++)
{
System.out.print(b[i][d]);
System.out.print("");
}
System.out.print("
");
if ((i +1)%(int)Math.sqrt(n)==0)
{
System.out.print("");
}
}
}
// main method
public static void main(String argvs[])
{
// the 9 x 9 grid
int[][] b = new int[][]{{7,0,0,0,0,0,2,0,0},
{4,0,2,0,0,0,0,0,3},
{0,0,0,2,0,1,0,0,0},
{3,0,0,1,8,0,0,9,7},
{0,0,9,0,7,0,6,0,0},
{6,5,0,0,3,2,0,0,1},
{0,0,0,4,0,9,0,0,0},
{5,0,0,0,0,0,1,0,6},
{0,0,6,0,0,0,0,0,8}
} ;
SudokuPuzzle obj = new SudokuPuzzle();
int size = b.length;
System.out.println("The grid is: ");
for(int i =0; i < size; i++)
{
for(int j =0; j < size; j++)
{
System.out.print(b[i][j]+"");
}
System.out.println();
}
System.out.println();
if (obj.solveSudoku(b, size))
{
System.out.println("The solution of the grid is: ");
obj.display(b, size);
}
else
{
System.out.println("There is no solution available.");
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions