回答編集履歴
2
alg が関数値として件数を返すように修正
test
CHANGED
@@ -7,17 +7,17 @@
|
|
7
7
|
//表示
|
8
8
|
void print(int *table) {
|
9
9
|
for(int i=0; i<N; i++) putchar(table[i] + '0');
|
10
|
-
putchar('\n');
|
10
|
+
putchar('\n');
|
11
11
|
}
|
12
|
-
//横に衝突
|
12
|
+
// i と j が横に衝突していたら !0
|
13
13
|
int collisionSideways(int *table, int i, int j) {
|
14
|
-
return table[i] == table[j];
|
14
|
+
return table[i] == table[j]; //行が同じ
|
15
15
|
}
|
16
|
-
//斜めに衝突
|
16
|
+
// i と j が斜めに衝突していたら !0
|
17
17
|
int collisionDiagonally(int *table, int i, int j) {
|
18
|
-
return abs(table[i] - table[j]) == abs(i - j);
|
18
|
+
return abs(table[i] - table[j]) == abs(i - j); //行と列のそれぞれの差の絶対値が同じなら衝突 Ex: (1,2)-(3,4), (1,7)-(4,4)
|
19
19
|
}
|
20
|
-
//クイーン衝突判定
|
20
|
+
//クイーン衝突判定(column と 0~column-1 の何れかとが衝突していたら !0)
|
21
21
|
int judgment(int *table, int column) {
|
22
22
|
int collision = 0; //衝突無
|
23
23
|
for(int j=0; j<column && !collision; j++) {
|
@@ -26,24 +26,23 @@
|
|
26
26
|
return collision;
|
27
27
|
}
|
28
28
|
|
29
|
-
|
29
|
+
int alg(int *table, int column) {
|
30
30
|
if(column == N) { //全部終わっていたら表示
|
31
31
|
print(table);
|
32
|
-
(*count)++;
|
33
|
-
return;
|
32
|
+
return 1;
|
34
33
|
}
|
35
34
|
|
35
|
+
int count = 0;
|
36
36
|
for(int row=0; row<N; row++) {
|
37
37
|
table[column] = row;
|
38
|
-
if(!judgment(table, column)) alg(table, column+1
|
38
|
+
if(!judgment(table, column)) count += alg(table, column+1); //衝突していなければ次の列
|
39
39
|
}
|
40
|
+
return count;
|
40
41
|
}
|
41
42
|
|
42
43
|
int main(void) {
|
43
44
|
int table[N];
|
44
|
-
int count = 0;
|
45
|
-
alg(table, 0, &count);
|
46
|
-
printf("count=%d\n",
|
45
|
+
printf("count=%d\n", alg(table, 0));
|
47
46
|
}
|
48
47
|
```
|
49
48
|
実行結果
|
1
追加
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
弄り過ぎて別物になってしまってます・・・。
|
1
|
+
弄り過ぎて(見た目)別物になってしまってます・・・。
|
2
2
|
```c
|
3
3
|
#include <stdio.h>
|
4
4
|
|
@@ -46,3 +46,18 @@
|
|
46
46
|
printf("count=%d\n", count);
|
47
47
|
}
|
48
48
|
```
|
49
|
+
実行結果
|
50
|
+
```plain
|
51
|
+
0257948136
|
52
|
+
0258693147
|
53
|
+
0258693174
|
54
|
+
0286931475
|
55
|
+
0358297146
|
56
|
+
(中略)
|
57
|
+
9641702853
|
58
|
+
9713068524
|
59
|
+
9741306825
|
60
|
+
9741306852
|
61
|
+
9742051863
|
62
|
+
count=724
|
63
|
+
```
|