-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueens for N combinations
169 lines (147 loc) · 2.67 KB
/
Queens for N combinations
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
public class Queen
{
private int size = 0;
int atleastOnce = 0;
boolean endOfBoard = false;
public Queen(int n)
{
size = n;
}
public void QP1(int board[][], int rows, int cols, int qc)
{
QP(board,rows,cols,qc);
}
public boolean QP(int board[][], int rows, int cols, int qc)
{
if(qc == 0)
{
pintBoard(board);
atleastOnce += 1;
return false;
}
else
{
for(int i = 0; i < size; i++)
{
if(checkIfFeasibleposition(board,rows,i))
{
board[rows][i] = 1;
if(QP(board, rows+1, 0, qc-1))
return true;
else
board[rows][i] = 0;
}
else
{
board[rows][i] = 0;
}
}
if(qc ==0)
endOfBoard = true;
}
if(endOfBoard == true)
return true;
return false;
}
private void pintBoard(int[][] board) {
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
System.out.print("| " + board[i][j]);
}
System.out.println( "|" );
}
System.out.println( "-------------------------------" );
}
private boolean checkIfFeasibleposition(int[][] board, int rows, int cols)
{
if(! rowsCheck(rows,board))
return false;
else if(!columnCheck(cols,board))
return false;
else if(!diagonalCheck(rows,cols,board))
return false;
else
return true;
}
private boolean diagonalCheck(int rows, int cols,int board[][])
{
if( !checkForwardDiagonal(board,rows,cols))
return false;
else if( !checkBackwardDiagonal(board,rows,cols))
return false;
else
return true;
}
private boolean checkBackwardDiagonal(int[][] board, int rows1, int cols1)
{
int rows = rows1;
int cols = cols1;
while(rows < size && cols >= 0)
{
if(board[rows1][cols1] == 1)
{
return false;
}
rows = rows + 1;
cols = cols-1;
}
while(rows1 >= 0 && cols1 < size)
{
if(board[rows1][cols1] == 1)
{
return false;
}
rows1 = rows1 - 1;
cols1 = cols1 + 1;
}
return true;
}
private boolean checkForwardDiagonal(int[][] board, int rows1, int cols1)
{
int rows = rows1;
int cols = cols1;
while(rows < size && cols < size)
{
if(board[rows][cols] == 1)
{
return false;
}
rows = rows + 1;
cols = cols + 1;
}
while(rows1 >= 0 && cols1 >= 0)
{
if(board[rows1][cols1] == 1)
{
return false;
}
rows1 = rows1 - 1;
cols1 = cols1 - 1;
}
return true;
}
private boolean columnCheck(int cols,int board[][])
{
for(int i = 0; i<size;i++)
{
if(board[i][cols] == 1)
{
return false;
}
}
return true;
}
private boolean rowsCheck(int rows,int board[][])
{
for(int i = 0; i<size;i++)
{
if(board[rows][i] == 1)
{
return false;
}
}
return true;
}
}