File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed
Challenges_VIII/Little_Bishops Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ package Challenges_VIII .Little_Bishops ;
2
+
3
+ import java .util .Scanner ;
4
+
5
+ public class Main {
6
+
7
+ public static int numSolutions = 0 ;
8
+
9
+ public static void main (String [] args ) {
10
+ Scanner s = new Scanner (System .in );
11
+
12
+ int boardSize = 0 ;
13
+ int maxBishops = 0 ;
14
+
15
+ while ((boardSize = s .nextInt ()) != 0 && (maxBishops = s .nextInt ()) != 0 ) {
16
+ numSolutions = 0 ;
17
+ int [] equations = new int [maxBishops ];
18
+ backtrack (equations , 1 , maxBishops , boardSize );
19
+
20
+ System .out .println (numSolutions );
21
+ }
22
+
23
+ }
24
+
25
+ public static void backtrack (int [] equations , int numBishops , int maxBishops , int boardSize ) {
26
+ if (numBishops < maxBishops ) {
27
+ for (int x = 0 ; x < boardSize ; x ++) {
28
+ for (int y = 0 ; y < boardSize ; y ++) {
29
+ boolean blocked = false ;
30
+ for (int e : equations ) {
31
+ if (((-1 * x ) + e ) == y || (x - e ) == y ) {
32
+ blocked = true ;
33
+ break ;
34
+ }
35
+ }
36
+
37
+ if (!blocked ) {
38
+ if (numBishops < maxBishops ) {
39
+ int [] temp = equations ;
40
+ temp [numBishops ] = y - x ;
41
+ backtrack (temp , numBishops + 1 , maxBishops , boardSize );
42
+ }
43
+ // if (numBishops == maxBishops) {
44
+ // numSolutions++;
45
+ // }
46
+ }
47
+ }
48
+ }
49
+ } else {
50
+ numSolutions ++;
51
+
52
+ }
53
+
54
+ }
55
+
56
+ }
You can’t perform that action at this time.
0 commit comments