Eight Queens Problem
You need to find all possible solutions to place eight queens on a chess board - Condition is no two queens should be able to collide.
Note: Queen can move diagonally or linearly to any length.
int q1 = 0;
int q2 = 0;
int q3 = 0;
int q4 = 0;
int q5 = 0;
int q6 = 0;
int q7 = 0;
int q8 = 0;
int counter = 0;
for ( q1 = 1; q1 < 9; q1++)
{
for ( q2 = 1; q2 < 9; q2++)
{
if ((q1 != q2)&& ( Math.Abs(q1 - q2) != 1 ) )
{
for ( q3 = 1; q3 < 9; q3++)
{
if ((q1 != q3) && (q2 != q3) && (Math.Abs(q1 - q3) != 2) && (Math.Abs(q2 - q3) != 1))
{
for ( q4 = 1; q4 < 9; q4++)
{
if ((q1 != q4) && (q2 != q4) && (q3 != q4) && (Math.Abs(q1 - q4) != 3) && (Math.Abs(q2 - q4) != 2) && (Math.Abs(q3 - q4) != 1))
{
for ( q5 = 1; q5 < 9; q5++)
{
if ((q1 != q5) && (q2 != q5) && (q3 != q5) && (q4 != q5) && (Math.Abs(q1 - q5) != 4) && (Math.Abs(q2 - q5) != 3) && (Math.Abs(q3 - q5) != 2) && (Math.Abs(q4 - q5) != 1))
{
for ( q6 = 1; q6 < 9; q6++)
{
if ((q1 != q6) && (q2 != q6) && (q3 != q6) && (q4 != q6) && (q5 != q6)
&& (Math.Abs(q1 - q6) != 5)
&& (Math.Abs(q2 - q6) != 4)
&& (Math.Abs(q3 - q6) != 3)
&& (Math.Abs(q4 - q6) != 2)
&& (Math.Abs(q5 - q6) != 1))
{
for ( q7 = 1; q7 < 9; q7++)
{
if ((q1 != q7) && (q2 != q7) && (q3 != q7) && (q4 != q7) && (q5 != q7) && (q6 != q7)
&& (Math.Abs(q1 - q7) != 6)
&& (Math.Abs(q2 - q7) != 5)
&& (Math.Abs(q3 - q7) != 4)
&& (Math.Abs(q4 - q7) != 3)
&& (Math.Abs(q5 - q7) != 2)
&& (Math.Abs(q6 - q7) != 1))
{
for ( q8 = 1; q8 < 9; q8++)
{
if ((q1 != q8) && (q2 != q8) && (q3 != q8) && (q4 != q8) && (q5 != q8) && (q6 != q8) && (q7 != q8)
&& (Math.Abs(q1 - q8) != 7)
&& (Math.Abs(q2 - q8) != 6)
&& (Math.Abs(q3 - q8) != 5)
&& (Math.Abs(q4 - q8) != 4)
&& (Math.Abs(q5 - q8) != 3)
&& (Math.Abs(q6 - q8) != 2)
&& (Math.Abs(q7 - q8) != 1))
{
counter++;
answer += counter.ToString() + ": " +
"(1," + q1.ToString() + "),"+
"(2," + q2.ToString() + ")," +
"(3," + q3.ToString() + ")," +
"(4," + q4.ToString() + ")," +
"(5," + q5.ToString() + ")," +
"(6," + q6.ToString() + ")," +
"(7," + q7.ToString() + ")," +
"(8," + q8.ToString() + ")" + "\n\r";
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
MessageBox.Show(answer );
label2.Text = answer;
1: (1,1),(2,5),(3,8),(4,6),(5,3),(6,7),(7,2),(8,4)
2: (1,1),(2,6),(3,8),(4,3),(5,7),(6,4),(7,2),(8,5)
3: (1,1),(2,7),(3,4),(4,6),(5,8),(6,2),(7,5),(8,3)
4: (1,1),(2,7),(3,5),(4,8),(5,2),(6,4),(7,6),(8,3)
5: (1,2),(2,4),(3,6),(4,8),(5,3),(6,1),(7,7),(8,5)
6: (1,2),(2,5),(3,7),(4,1),(5,3),(6,8),(7,6),(8,4)
7: (1,2),(2,5),(3,7),(4,4),(5,1),(6,8),(7,6),(8,3)
8: (1,2),(2,6),(3,1),(4,7),(5,4),(6,8),(7,3),(8,5)
9: (1,2),(2,6),(3,8),(4,3),(5,1),(6,4),(7,7),(8,5)
10: (1,2),(2,7),(3,3),(4,6),(5,8),(6,5),(7,1),(8,4)
11: (1,2),(2,7),(3,5),(4,8),(5,1),(6,4),(7,6),(8,3)
12: (1,2),(2,8),(3,6),(4,1),(5,3),(6,5),(7,7),(8,4)
13: (1,3),(2,1),(3,7),(4,5),(5,8),(6,2),(7,4),(8,6)
14: (1,3),(2,5),(3,2),(4,8),(5,1),(6,7),(7,4),(8,6)
15: (1,3),(2,5),(3,2),(4,8),(5,6),(6,4),(7,7),(8,1)
16: (1,3),(2,5),(3,7),(4,1),(5,4),(6,2),(7,8),(8,6)
17: (1,3),(2,5),(3,8),(4,4),(5,1),(6,7),(7,2),(8,6)
18: (1,3),(2,6),(3,2),(4,5),(5,8),(6,1),(7,7),(8,4)
19: (1,3),(2,6),(3,2),(4,7),(5,1),(6,4),(7,8),(8,5)
20: (1,3),(2,6),(3,2),(4,7),(5,5),(6,1),(7,8),(8,4)
21: (1,3),(2,6),(3,4),(4,1),(5,8),(6,5),(7,7),(8,2)
22: (1,3),(2,6),(3,4),(4,2),(5,8),(6,5),(7,7),(8,1)
23: (1,3),(2,6),(3,8),(4,1),(5,4),(6,7),(7,5),(8,2)
24: (1,3),(2,6),(3,8),(4,1),(5,5),(6,7),(7,2),(8,4)
25: (1,3),(2,6),(3,8),(4,2),(5,4),(6,1),(7,7),(8,5)
26: (1,3),(2,7),(3,2),(4,8),(5,5),(6,1),(7,4),(8,6)
27: (1,3),(2,7),(3,2),(4,8),(5,6),(6,4),(7,1),(8,5)
28: (1,3),(2,8),(3,4),(4,7),(5,1),(6,6),(7,2),(8,5)
29: (1,4),(2,1),(3,5),(4,8),(5,2),(6,7),(7,3),(8,6)
30: (1,4),(2,1),(3,5),(4,8),(5,6),(6,3),(7,7),(8,2)
31: (1,4),(2,2),(3,5),(4,8),(5,6),(6,1),(7,3),(8,7)
32: (1,4),(2,2),(3,7),(4,3),(5,6),(6,8),(7,1),(8,5)
33: (1,4),(2,2),(3,7),(4,3),(5,6),(6,8),(7,5),(8,1)
34: (1,4),(2,2),(3,7),(4,5),(5,1),(6,8),(7,6),(8,3)
35: (1,4),(2,2),(3,8),(4,5),(5,7),(6,1),(7,3),(8,6)
36: (1,4),(2,2),(3,8),(4,6),(5,1),(6,3),(7,5),(8,7)
37: (1,4),(2,6),(3,1),(4,5),(5,2),(6,8),(7,3),(8,7)
38: (1,4),(2,6),(3,8),(4,2),(5,7),(6,1),(7,3),(8,5)
39: (1,4),(2,6),(3,8),(4,3),(5,1),(6,7),(7,5),(8,2)
40: (1,4),(2,7),(3,1),(4,8),(5,5),(6,2),(7,6),(8,3)
41: (1,4),(2,7),(3,3),(4,8),(5,2),(6,5),(7,1),(8,6)
42: (1,4),(2,7),(3,5),(4,2),(5,6),(6,1),(7,3),(8,8)
43: (1,4),(2,7),(3,5),(4,3),(5,1),(6,6),(7,8),(8,2)
44: (1,4),(2,8),(3,1),(4,3),(5,6),(6,2),(7,7),(8,5)
45: (1,4),(2,8),(3,1),(4,5),(5,7),(6,2),(7,6),(8,3)
46: (1,4),(2,8),(3,5),(4,3),(5,1),(6,7),(7,2),(8,6)
47: (1,5),(2,1),(3,4),(4,6),(5,8),(6,2),(7,7),(8,3)
48: (1,5),(2,1),(3,8),(4,4),(5,2),(6,7),(7,3),(8,6)
49: (1,5),(2,1),(3,8),(4,6),(5,3),(6,7),(7,2),(8,4)
50: (1,5),(2,2),(3,4),(4,6),(5,8),(6,3),(7,1),(8,7)
51: (1,5),(2,2),(3,4),(4,7),(5,3),(6,8),(7,6),(8,1)
52: (1,5),(2,2),(3,6),(4,1),(5,7),(6,4),(7,8),(8,3)
53: (1,5),(2,2),(3,8),(4,1),(5,4),(6,7),(7,3),(8,6)
54: (1,5),(2,3),(3,1),(4,6),(5,8),(6,2),(7,4),(8,7)
55: (1,5),(2,3),(3,1),(4,7),(5,2),(6,8),(7,6),(8,4)
56: (1,5),(2,3),(3,8),(4,4),(5,7),(6,1),(7,6),(8,2)
57: (1,5),(2,7),(3,1),(4,3),(5,8),(6,6),(7,4),(8,2)
58: (1,5),(2,7),(3,1),(4,4),(5,2),(6,8),(7,6),(8,3)
59: (1,5),(2,7),(3,2),(4,4),(5,8),(6,1),(7,3),(8,6)
60: (1,5),(2,7),(3,2),(4,6),(5,3),(6,1),(7,4),(8,8)
61: (1,5),(2,7),(3,2),(4,6),(5,3),(6,1),(7,8),(8,4)
62: (1,5),(2,7),(3,4),(4,1),(5,3),(6,8),(7,6),(8,2)
63: (1,5),(2,8),(3,4),(4,1),(5,3),(6,6),(7,2),(8,7)
64: (1,5),(2,8),(3,4),(4,1),(5,7),(6,2),(7,6),(8,3)
65: (1,6),(2,1),(3,5),(4,2),(5,8),(6,3),(7,7),(8,4)
66: (1,6),(2,2),(3,7),(4,1),(5,3),(6,5),(7,8),(8,4)
67: (1,6),(2,2),(3,7),(4,1),(5,4),(6,8),(7,5),(8,3)
68: (1,6),(2,3),(3,1),(4,7),(5,5),(6,8),(7,2),(8,4)
69: (1,6),(2,3),(3,1),(4,8),(5,4),(6,2),(7,7),(8,5)
70: (1,6),(2,3),(3,1),(4,8),(5,5),(6,2),(7,4),(8,7)
71: (1,6),(2,3),(3,5),(4,7),(5,1),(6,4),(7,2),(8,8)
72: (1,6),(2,3),(3,5),(4,8),(5,1),(6,4),(7,2),(8,7)
73: (1,6),(2,3),(3,7),(4,2),(5,4),(6,8),(7,1),(8,5)
74: (1,6),(2,3),(3,7),(4,2),(5,8),(6,5),(7,1),(8,4)
75: (1,6),(2,3),(3,7),(4,4),(5,1),(6,8),(7,2),(8,5)
76: (1,6),(2,4),(3,1),(4,5),(5,8),(6,2),(7,7),(8,3)
77: (1,6),(2,4),(3,2),(4,8),(5,5),(6,7),(7,1),(8,3)
78: (1,6),(2,4),(3,7),(4,1),(5,3),(6,5),(7,2),(8,8)
79: (1,6),(2,4),(3,7),(4,1),(5,8),(6,2),(7,5),(8,3)
80: (1,6),(2,8),(3,2),(4,4),(5,1),(6,7),(7,5),(8,3)
81: (1,7),(2,1),(3,3),(4,8),(5,6),(6,4),(7,2),(8,5)
82: (1,7),(2,2),(3,4),(4,1),(5,8),(6,5),(7,3),(8,6)
83: (1,7),(2,2),(3,6),(4,3),(5,1),(6,4),(7,8),(8,5)
84: (1,7),(2,3),(3,1),(4,6),(5,8),(6,5),(7,2),(8,4)
85: (1,7),(2,3),(3,8),(4,2),(5,5),(6,1),(7,6),(8,4)
86: (1,7),(2,4),(3,2),(4,5),(5,8),(6,1),(7,3),(8,6)
87: (1,7),(2,4),(3,2),(4,8),(5,6),(6,1),(7,3),(8,5)
88: (1,7),(2,5),(3,3),(4,1),(5,6),(6,8),(7,2),(8,4)
89: (1,8),(2,2),(3,4),(4,1),(5,7),(6,5),(7,3),(8,6)
90: (1,8),(2,2),(3,5),(4,3),(5,1),(6,7),(7,4),(8,6)
91: (1,8),(2,3),(3,1),(4,6),(5,2),(6,5),(7,7),(8,4)
92: (1,8),(2,4),(3,1),(4,3),(5,6),(6,2),(7,7),(8,5)
Here are details of problem .
1) imagine rows 1,2,3,4,5,6,7,8
2) Now you want to place queens on 8 rows one in each row( one queeen per row :part of queens scope)
3) so each can be placed in any of 8 columns of each row.
4) when you place each one check it is not hitting any previously positioned queen.
5) Rest try to understand the code.
improve:
Int can be replaced with tiny int. (one byte can represent 8 possibilities)
This is a processor intesive and memory conservative algorithm.
Bad: hardcoded 8 alla across.
N queens on NXN square will be good problem to solve.