Skip to content

Commit 1b2623c

Browse files
authored
Merge pull request #268 from techfreakSahil/techsahil2
[JAVA] maze solver
2 parents 4e959de + 5a01b18 commit 1b2623c

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

Coding/Java/Maze.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.Scanner;
2+
3+
public class Maze {
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in);
6+
System.out.println("Enter the value of n: ");
7+
int n = sc.nextInt();
8+
boolean[][] board = new boolean[n][n];
9+
System.out.println(nQueens(board,0));
10+
}
11+
static int nQueens(boolean[][] board, int row){
12+
if(row==board.length){
13+
display(board);
14+
System.out.println();
15+
return 1;
16+
}
17+
int count = 0;
18+
for(int col =0;col<board.length;col++){
19+
if(isSafe(board, row, col)){
20+
board[row][col] = true;
21+
count += nQueens(board,row+1);
22+
board[row][col] = false;
23+
}
24+
}
25+
return count;
26+
}
27+
28+
static boolean isSafe(boolean[][] board, int row, int col){
29+
30+
for(int i=0;i<row;i++){
31+
if(board[i][col]) {
32+
return false;
33+
}
34+
}
35+
36+
int maxLeft = Math.min(row,col);
37+
38+
for(int i=1;i<=maxLeft;i++){
39+
if(board[row-i][col-i]){
40+
return false;
41+
}
42+
}
43+
44+
int maxRight = Math.min(row, board.length - col - 1);
45+
46+
for(int i=1;i<=maxRight;i++){
47+
if(board[row-i][col+i]){
48+
return false;
49+
}
50+
}
51+
52+
return true;
53+
}
54+
static void display(boolean[][] board){
55+
for(boolean[] row : board){
56+
for(boolean element : row){
57+
if(element){
58+
System.out.print("Q ");
59+
}
60+
else{
61+
System.out.print("X ");
62+
}
63+
}
64+
System.out.println();
65+
}
66+
}
67+
}

Coding/Java/Sudoko.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
public class Sudoku {
2+
public static void main(String[] args) {
3+
int[][] board = new int[][]{
4+
{0, 0, 6, 5, 0, 8, 4, 0, 0},
5+
{5, 2, 0, 0, 0, 0, 0, 0, 0},
6+
{0, 8, 7, 0, 0, 0, 0, 3, 1},
7+
{0, 0, 3, 0, 1, 0, 0, 8, 0},
8+
{9, 0, 0, 8, 6, 3, 0, 0, 5},
9+
{0, 5, 0, 0, 9, 0, 6, 0, 0},
10+
{1, 3, 0, 0, 0, 0, 2, 5, 0},
11+
{0, 0, 0, 0, 0, 0, 0, 7, 4},
12+
{0, 0, 5, 2, 0, 6, 3, 0, 0}
13+
};
14+
15+
if (solve(board)) {
16+
display(board);
17+
} else {
18+
System.out.println("Cannot solve");
19+
}
20+
21+
}
22+
23+
static boolean solve(int[][] board) {
24+
int n = board.length;
25+
int row = -1;
26+
int col = -1;
27+
28+
boolean emptyLeft = true;
29+
30+
// this is how we are replacing the r,c from arguments
31+
for (int i = 0; i < n; i++) {
32+
for (int j = 0; j < n; j++) {
33+
if (board[i][j] == 0) {
34+
row = i;
35+
col = j;
36+
emptyLeft = false;
37+
break;
38+
}
39+
}
40+
// if you found some empty element in row, then break
41+
if (emptyLeft == false) {
42+
break;
43+
}
44+
}
45+
46+
if (emptyLeft == true) {
47+
return true;
48+
// soduko is solved
49+
}
50+
51+
// backtrack
52+
for (int number = 1; number <= 9; number++) {
53+
if (isSafe(board, row, col, number)) {
54+
board[row][col] = number;
55+
if (solve(board)) {
56+
// found the answer
57+
return true;
58+
} else {
59+
// backtrack
60+
board[row][col] = 0;
61+
}
62+
}
63+
}
64+
return false;
65+
}
66+
67+
private static void display(int[][] board) {
68+
for(int[] row : board) {
69+
for(int num : row) {
70+
System.out.print(num + " ");
71+
}
72+
System.out.println();
73+
}
74+
}
75+
76+
77+
static boolean isSafe(int[][] board, int row, int col, int num) {
78+
// check the row
79+
for (int i = 0; i < board.length; i++) {
80+
// check if the number is in the row
81+
if (board[row][i] == num) {
82+
return false;
83+
}
84+
}
85+
86+
// check the col
87+
for (int[] nums : board) {
88+
// check if the number is in the col
89+
if (nums[col] == num) {
90+
return false;
91+
}
92+
}
93+
94+
int sqrt = (int)(Math.sqrt(board.length));
95+
int rowStart = row - row % sqrt;
96+
int colStart = col - col % sqrt;
97+
98+
for (int r = rowStart; r < rowStart + sqrt; r++) {
99+
for (int c = colStart; c < colStart + sqrt; c++) {
100+
if (board[r][c] == num) {
101+
return false;
102+
}
103+
}
104+
}
105+
return true;
106+
}
107+
}

0 commit comments

Comments
 (0)