Skip to content

Commit

Permalink
Added java code and readme updated (#295)
Browse files Browse the repository at this point in the history
* Added java code and readme updated

* Update README.md

Co-authored-by: Sifat <hossain0338@gmail.com>
  • Loading branch information
Rajesh144142 and shhossain committed Oct 19, 2022
1 parent eaed17f commit 80edc79
Showing 1 changed file with 134 additions and 1 deletion.
135 changes: 134 additions & 1 deletion Algorithms/Backtracking/Rat in a Maze/README.md
Expand Up @@ -159,10 +159,143 @@ int main() {
```
<br>
<h3> Complexity Analysis </h3>
<p><h> Time Complexity: </h>
<p><h3> Time Complexity: </h3>
<br>
O(9^k) where k is the number of unfilled elements. <br>
<h>Space Complexity: </h>
<br>
O(n*n) where n is the number of rows and columns of the sudoku 2D matrix
## JAVA CODE
```java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static int N = 9;
// Bitmasks for each row/column/box
static int row[] = new int[N], col[] = new int[N],
box[] = new int[N];
static Boolean seted = false;
// Utility function to find the box index
// of an element at position [i][j] in the grid
static int getBox(int i, int j)
{
return i / 3 * 3 + j / 3;
}
// Utility function to check if a number
// is present in the corresponding row/column/box
static Boolean isSafe(int i, int j, int number)
{
return ((row[i] >> number) & 1) == 0
&& ((col[j] >> number) & 1) == 0
&& ((box[getBox(i, j)] >> number) & 1) == 0;
}
// Utility function to set the initial values of a
// Sudoku board (map the values in the bitmasks)
static void setInitialValues(int grid[][])
{
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
row[i] |= 1 << grid[i][j];
col[j] |= 1 << grid[i][j];
box[getBox(i, j)] |= 1 << grid[i][j];
}
}
}
/* Takes a partially filled-in grid and attempts
to assign values to all unassigned locations in
such a way to meet the requirements for
Sudoku solution (non-duplication across rows,
columns, and boxes) */
static Boolean SolveSudoku(int grid[][], int i, int j)
{
// Set the initial values
if (!seted) {
seted = true;
setInitialValues(grid);
}
if (i == N - 1 && j == N)
return true;
if (j == N) {
j = 0;
i++;
}
if (grid[i][j] > 0)
return SolveSudoku(grid, i, j + 1);
for (int nr = 1; nr <= N; nr++) {
if (isSafe(i, j, nr)) {
/* Assign nr in the
current (i, j)
position and
add nr to each bitmask
*/
grid[i][j] = nr;
row[i] |= 1 << nr;
col[j] |= 1 << nr;
box[getBox(i, j)] |= 1 << nr;
if (SolveSudoku(grid, i, j + 1))
return true;
// Remove nr from each bitmask
// and search for another possibility
row[i] &= ~(1 << nr);
col[j] &= ~(1 << nr);
box[getBox(i, j)] &= ~(1 << nr);
}
grid[i][j] = 0;
}
return false;
}
// Utility function to print the solved grid
static void print(int grid[][])
{
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
System.out.printf("%d ", grid[i][j]);
}
System.out.println();
}
}
// Driver code
public static void main(String args[])
{
// 0 means unassigned cells
int grid[][] = { { 3, 0, 6, 5, 0, 8, 4, 0, 0 },
{ 5, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 8, 7, 0, 0, 0, 0, 3, 1 },
{ 0, 0, 3, 0, 1, 0, 0, 8, 0 },
{ 9, 0, 0, 8, 6, 3, 0, 0, 5 },
{ 0, 5, 0, 0, 9, 0, 6, 0, 0 },
{ 1, 3, 0, 0, 0, 0, 2, 5, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 7, 4 },
{ 0, 0, 5, 2, 0, 6, 3, 0, 0 } };
if (SolveSudoku(grid, 0, 0))
print(grid);
else
System.out.println("No solution exists");
}
}
```



0 comments on commit 80edc79

Please sign in to comment.