-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEightPuzzelQueen.cs
More file actions
103 lines (79 loc) · 2.78 KB
/
Copy pathEightPuzzelQueen.cs
File metadata and controls
103 lines (79 loc) · 2.78 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EightPuzzelQueen
{
public class EightPuzzleQueen2
{
public static int SixQueen()
{
int[] columnIndex = new int[6] { 0, 1, 2, 3, 4, 5 };
int count = 0;
permutation(columnIndex, 6, 0, ref count);
return count;
}
public static int EightQueen()
{
int[] columnIndex = new int[8] { 0, 1, 2, 3, 4, 5, 6, 7 };
int count = 0;
permutation(columnIndex, 8, 0, ref count);
return count;
}
public static int TenQueen()
{
int[] columnIndex = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int count = 0;
permutation(columnIndex, 10, 0, ref count);
return count;
}
// using back track algorithm to check one by one, and go through all the cases
public static void permutation(int[] columnIndex, int length, int index, ref int count)
{
int i, temp;
// terminal case, go through the last row already
if (index == length)
{
if (check(columnIndex, length) != 0)
count++;
}
else
{
for (i = index; i < length; ++i)
{
// swap value at two columns, i and index
temp = columnIndex[i];
columnIndex[i] = columnIndex[index];
columnIndex[index] = temp;
permutation(columnIndex, length, index + 1, ref count);
// back track, and, swap value at two columns, i and index
temp = columnIndex[index];
columnIndex[index] = columnIndex[i];
columnIndex[i] = temp;
}
}
}
// If there are two queens on the diagonal, it returns 0, otherwise it returns 1
public static int check(int[] columnIndex, int length)
{
// nxn comparison,
for(int i=0;i<length; i++)
for (int j = i + 1; j < length; j++)
{
if(((i+columnIndex[i])==(j+columnIndex[j]) || (columnIndex[i]-columnIndex[j])==(i-j)))
return 0;
}
return 1;
}
static void Main(string[] args)
{
int count = EightQueen();
//int count10 = EightPuzzelQueen.EightPuzzleQueen2.TenQueen();
// int count6 = EightPuzzelQueen.EightPuzzleQueen2.SixQueen();
}
}
public class EightPuzzelQueen
{
}
}