Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Coding/Java/Maze.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.Scanner;

public class Maze {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n: ");
int n = sc.nextInt();
boolean[][] board = new boolean[n][n];
System.out.println(nQueens(board,0));
}
static int nQueens(boolean[][] board, int row){
if(row==board.length){
display(board);
System.out.println();
return 1;
}
int count = 0;
for(int col =0;col<board.length;col++){
if(isSafe(board, row, col)){
board[row][col] = true;
count += nQueens(board,row+1);
board[row][col] = false;
}
}
return count;
}

static boolean isSafe(boolean[][] board, int row, int col){

for(int i=0;i<row;i++){
if(board[i][col]) {
return false;
}
}

int maxLeft = Math.min(row,col);

for(int i=1;i<=maxLeft;i++){
if(board[row-i][col-i]){
return false;
}
}

int maxRight = Math.min(row, board.length - col - 1);

for(int i=1;i<=maxRight;i++){
if(board[row-i][col+i]){
return false;
}
}

return true;
}
static void display(boolean[][] board){
for(boolean[] row : board){
for(boolean element : row){
if(element){
System.out.print("Q ");
}
else{
System.out.print("X ");
}
}
System.out.println();
}
}
}
107 changes: 107 additions & 0 deletions Coding/Java/Sudoko.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
public class Sudoku {
public static void main(String[] args) {
int[][] board = new int[][]{
{0, 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 (solve(board)) {
display(board);
} else {
System.out.println("Cannot solve");
}

}

static boolean solve(int[][] board) {
int n = board.length;
int row = -1;
int col = -1;

boolean emptyLeft = true;

// this is how we are replacing the r,c from arguments
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 0) {
row = i;
col = j;
emptyLeft = false;
break;
}
}
// if you found some empty element in row, then break
if (emptyLeft == false) {
break;
}
}

if (emptyLeft == true) {
return true;
// soduko is solved
}

// backtrack
for (int number = 1; number <= 9; number++) {
if (isSafe(board, row, col, number)) {
board[row][col] = number;
if (solve(board)) {
// found the answer
return true;
} else {
// backtrack
board[row][col] = 0;
}
}
}
return false;
}

private static void display(int[][] board) {
for(int[] row : board) {
for(int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}


static boolean isSafe(int[][] board, int row, int col, int num) {
// check the row
for (int i = 0; i < board.length; i++) {
// check if the number is in the row
if (board[row][i] == num) {
return false;
}
}

// check the col
for (int[] nums : board) {
// check if the number is in the col
if (nums[col] == num) {
return false;
}
}

int sqrt = (int)(Math.sqrt(board.length));
int rowStart = row - row % sqrt;
int colStart = col - col % sqrt;

for (int r = rowStart; r < rowStart + sqrt; r++) {
for (int c = colStart; c < colStart + sqrt; c++) {
if (board[r][c] == num) {
return false;
}
}
}
return true;
}
}