forked from goyalavishi/Practice-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NQueenProblem.java
110 lines (91 loc) · 2.21 KB
/
NQueenProblem.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.Arrays;
class NQueenProblem
{
public int iterations=0;
static int x;
static int[][] board;
NQueenProblem(String arr)
{
//initializing array
x=Integer.parseInt(arr);
board=new int[x][x];
}
boolean isQueenProtected(int row,int col)
{
// Check side columns of same row
for (int i = col-1; i >=0; i--)
if (board[row][i] == 1)
{
System.out.println("false");
return false;
}
// Check upper diagonal on left side
for (int i=row-1, j=col-1; i>=0 && j>=0; i--, j--)
if (board[i][j] == 1)
{
System.out.println("false");
return false;
}
//Check lower diagonal on left side
for (int i=row+1, j=col-1; j>=0 && i<x; i++, j--)
if (board[i][j] == 1)
{
System.out.println("false");
return false;
}
System.out.println("true");
return true;
}
boolean solution(int col)
{
// If all queens are placed
if (col >= x)
return true;
System.out.println("column="+col);
for (int i = 0; i < x; i++)
{
if (isQueenProtected(i, col))
{
// if queen is safe Place this queen in board[i][col]
board[i][col] = 1;
// test for all columns
if (solution(col + 1) == true)
return true;
// If placing queen in board[i][col] doesn't lead to a solution then backtrack
board[i][col] = 0;
}
printboard();
}
return false;
}
public void printboard()
{
System.out.println("Iteration "+(iterations++)+": \n");
for(int i=0;i<x;i++)
{
for(int j=0;j<x;j++)
{
if(board[i][j]==1)
System.out.print("x ");
else
System.out.print("o ");
}
System.out.println("");
}
}
//Main Function
public static void main(String args[])
{
//initializing array
x=Integer.parseInt(args[0]);
board=new int[x][x];
//Arrays.fill(board,0);
NQueenProblem n=new NQueenProblem(args[0]);
n.printboard();
if (n.solution(0) == false)
{
System.out.print("Solution does not exist");
}
n.printboard();
}
}