Skip to content

Commit d6d43e6

Browse files
added n queen algorithm
1 parent d6c7ffd commit d6d43e6

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

nqueenproblem.java

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
public class NQueenProblem {
2+
final int N = 4;
3+
4+
/* A utility function to print solution */
5+
void printSolution(int board[][])
6+
{
7+
for (int i = 0; i < N; i++) {
8+
for (int j = 0; j < N; j++)
9+
System.out.print(" " + board[i][j]
10+
+ " ");
11+
System.out.println();
12+
}
13+
}
14+
15+
/* A utility function to check if a queen can
16+
be placed on board[row][col]. Note that this
17+
function is called when "col" queens are already
18+
placed in columns from 0 to col -1. So we need
19+
to check only left side for attacking queens */
20+
boolean isSafe(int board[][], int row, int col)
21+
{
22+
int i, j;
23+
24+
/* Check this row on left side */
25+
for (i = 0; i < col; i++)
26+
if (board[row][i] == 1)
27+
return false;
28+
29+
/* Check upper diagonal on left side */
30+
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
31+
if (board[i][j] == 1)
32+
return false;
33+
34+
/* Check lower diagonal on left side */
35+
for (i = row, j = col; j >= 0 && i < N; i++, j--)
36+
if (board[i][j] == 1)
37+
return false;
38+
39+
return true;
40+
}
41+
42+
/* A recursive utility function to solve N
43+
Queen problem */
44+
boolean solveNQUtil(int board[][], int col)
45+
{
46+
/* base case: If all queens are placed
47+
then return true */
48+
if (col >= N)
49+
return true;
50+
51+
/* Consider this column and try placing
52+
this queen in all rows one by one */
53+
for (int i = 0; i < N; i++) {
54+
/* Check if the queen can be placed on
55+
board[i][col] */
56+
if (isSafe(board, i, col)) {
57+
/* Place this queen in board[i][col] */
58+
board[i][col] = 1;
59+
60+
/* recur to place rest of the queens */
61+
if (solveNQUtil(board, col + 1) == true)
62+
return true;
63+
64+
/* If placing queen in board[i][col]
65+
doesn't lead to a solution then
66+
remove queen from board[i][col] */
67+
board[i][col] = 0; // BACKTRACK
68+
}
69+
}
70+
71+
/* If the queen can not be placed in any row in
72+
this column col, then return false */
73+
return false;
74+
}
75+
76+
/* This function solves the N Queen problem using
77+
Backtracking. It mainly uses solveNQUtil () to
78+
solve the problem. It returns false if queens
79+
cannot be placed, otherwise, return true and
80+
prints placement of queens in the form of 1s.
81+
Please note that there may be more than one
82+
solutions, this function prints one of the
83+
feasible solutions.*/
84+
boolean solveNQ()
85+
{
86+
int board[][] = { { 0, 0, 0, 0 },
87+
{ 0, 0, 0, 0 },
88+
{ 0, 0, 0, 0 },
89+
{ 0, 0, 0, 0 } };
90+
91+
if (solveNQUtil(board, 0) == false) {
92+
System.out.print("Solution does not exist");
93+
return false;
94+
}
95+
96+
printSolution(board);
97+
return true;
98+
}
99+
100+
// driver program to test above function
101+
public static void main(String args[])
102+
{
103+
NQueenProblem Queen = new NQueenProblem();
104+
Queen.solveNQ();
105+
}
106+
}

0 commit comments

Comments
 (0)