Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d2a45f8

Browse files
Create N Queens
1 parent fee27af commit d2a45f8

File tree

1 file changed

+105
-0
lines changed
  • Course 2 - Data Structures in JAVA/Lecture 14 - Backtracking

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
You are given N, and for a given N x N chessboard, find a way to place N queens such that no queen can attack any other queen on the chess board. A queen can be killed when it lies in the same row, or same column, or the same diagonal of any of the other queens. You have to print all such configurations.
3+
4+
Input Format :
5+
Line 1 : Integer N
6+
7+
Output Format :
8+
One Line for every board configuration.
9+
Every line will have N*N board elements printed row wise and are separated by space
10+
Note : Don't print anything if there isn't any valid configuration.
11+
12+
Constraints :
13+
1<=N<=10
14+
15+
Sample Input 1:
16+
4
17+
Sample Output 1 :
18+
0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0
19+
0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0
20+
*/
21+
22+
public class Solution {
23+
24+
25+
public static void placeNQueens(int n){
26+
27+
/* Your class should be named Solution.
28+
* Don't write main() function.
29+
* Don't read input, it is passed as function argument.
30+
* Print output as specified in the question
31+
*/
32+
int[][] board = new int[n][n];
33+
solveNQueens(board, 0,n);
34+
35+
}
36+
37+
static void solveNQueens(int board[][], int row, int N)
38+
{
39+
/* base case: If all queens are placed
40+
then return true */
41+
if (row == N)
42+
{
43+
printSolution(board,N);
44+
return;
45+
}
46+
47+
/* Consider this column and try placing
48+
this queen in all rows one by one */
49+
for (int i = 0; i < N; i++)
50+
{
51+
/* Check if queen can be placed on
52+
board[row][i] */
53+
if ( isSafe(board, row, i, N) )
54+
{
55+
/* Place this queen in board[row][i] */
56+
board[row][i] = 1;
57+
58+
// Make result true if any placement
59+
// is possible
60+
solveNQueens(board, row + 1, N);
61+
62+
/* If placing queen in board[row][i]
63+
doesn't lead to a solution, then backtrack and
64+
remove queen from board[row][i] */
65+
board[row][i] = 0;
66+
}
67+
}
68+
69+
}
70+
71+
static boolean isSafe(int board[][], int row, int col, int N)
72+
{
73+
int i, j;
74+
75+
//Check if all values in the given column and rows from 0 to row-1 are 0
76+
for (i=0;i<row;i++)
77+
{
78+
if (board[i][col]==1)
79+
return false;
80+
}
81+
82+
// Check upper diagonal on left side
83+
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
84+
if (board[i][j] == 1)
85+
return false;
86+
87+
//Check upper right diagonal
88+
for (i=row,j=col;i>=0 && j<N;i--,j++)
89+
if (board[i][j] == 1)
90+
return false;
91+
92+
return true;
93+
}
94+
95+
static void printSolution(int board[][], int N)
96+
{
97+
for (int i = 0; i < N; i++)
98+
{
99+
for (int j = 0; j < N; j++)
100+
System.out.print(board[i][j]+" ");
101+
}
102+
System.out.println();
103+
}
104+
}
105+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /