forked from Dipak3007/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacktracking.java
More file actions
74 lines (67 loc) · 2.29 KB
/
Backtracking.java
File metadata and controls
74 lines (67 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class Backtracking {
//N queens backtracking
final int n = 4;
//function to print answer
void backtrackAns(int board[][])
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.print(" " + board[i][j] + " ");
System.out.println();
}
}
boolean validate(int board[][], int rowInd, int colInd)
{
int i, j;
for (i = 0; i < colInd; i++)
if (board[rowInd][i] == 1)
return false;
//for upper diagonal on left side
for (i = rowInd, j = colInd; i >= 0 && j >= 0; i--, j--)
if (board[i][j] == 1)
return false;
//for lower diagonal on left side
for (i = rowInd, j = colInd; j >= 0 && i < n; i++, j--)
if (board[i][j] == 1)
return false;
return true;
}
//recursive function
boolean solveNqueens(int board[][], int col)
{
//if queens are placed then return true
if (col >= n)
return true;
for (int i = 0; i < n; i++) {
//if queen can be placed or not
if (validate(board, i, col)) {
board[i][col] = 1;
if (solveNqueens(board, col + 1) == true)
return true;
//if not then return false
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
//input to check the function
boolean constructNQ()
{
int board[][] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
if (solveNqueens(board, 0) == false) {
System.out.print("Failed");
return false;
}
backtrackAns(board);
return true;
}
//to test above function
public static void main(String args[])
{
Backtracking Queen = new Backtracking();
Queen.constructNQ();
}
}