Skip to content

Commit cc3d811

Browse files
committed
Update solutions
1 parent 3f29e40 commit cc3d811

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* (Game: Eight Queens) The classic Eight Queens puzzle is to place eight queens
3+
* on a chessboard such that no two queens can attack each other (i.e., no two queens
4+
* are on the same row, same column, or same diagonal). There are many possible
5+
* solutions. Write a program that displays one such solution. A sample output is
6+
* shown below:
7+
*
8+
* |Q| | | | | | | |
9+
* | | | | |Q| | | |
10+
* | | | | | | | |Q|
11+
* | | | | | |Q| | |
12+
* | | |Q| | | | | |
13+
* | | | | | | |Q| |
14+
* | |Q| | | | | | |
15+
* | | | |Q| | | | |
16+
*
17+
* ref:https://introcs.cs.princeton.edu/java/23recursion/Queens.java.html
18+
* Created by Sven on 08/29/19.
19+
*/
20+
package Chapter07;
21+
22+
public class Exercise0736_Game_Eight_Queens {
23+
public static void main(String[] args) {
24+
enumerate(8);
25+
}
26+
27+
/***************************************************************************
28+
* Return true if queen placement q[n] does not conflict with
29+
* other queens q[0] through q[n-1]
30+
***************************************************************************/
31+
public static boolean isConsistent(int[] q, int n) {
32+
for (int i = 0; i < n; i++) {
33+
if (q[i] == q[n]) return false; // same column
34+
if ((q[i] - q[n]) == (n - i)) return false; // same major diagonal
35+
if ((q[n] - q[i]) == (n - i)) return false; // same minor diagonal
36+
}
37+
return true;
38+
}
39+
40+
/***************************************************************************
41+
* Prints n-by-n placement of queens from permutation q in ASCII.
42+
***************************************************************************/
43+
public static void printQueens(int[] q) {
44+
int n = q.length;
45+
for (int i = 0; i < n; i++) {
46+
for (int j = 0; j < n; j++) {
47+
System.out.print("|");
48+
if (q[i] == j) System.out.print("Q");
49+
else System.out.print(" ");
50+
}
51+
System.out.println("|");
52+
}
53+
System.out.println();
54+
}
55+
56+
57+
/***************************************************************************
58+
* Try all permutations using backtracking
59+
***************************************************************************/
60+
public static void enumerate(int n) {
61+
int[] a = new int[n];
62+
enumerate(a, 0);
63+
}
64+
65+
public static void enumerate(int[] q, int k) {
66+
int n = q.length;
67+
if (k == n) printQueens(q);
68+
else {
69+
for (int i = 0; i < n; i++) {
70+
q[k] = i;
71+
if (isConsistent(q, k)) enumerate(q, k + 1);
72+
}
73+
}
74+
}
75+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* (Game: bean machine) The bean machine, also known as a quincunx or the Galton
3+
* box, is a device for statistics experiments named after English scientist Sir
4+
* Francis Galton. It consists of an upright board with evenly spaced nails (or pegs)
5+
* in a triangular form, as shown in Figure 7.13.
6+
*
7+
* Balls are dropped from the opening of the board. Every time a ball hits a nail, it
8+
* has a 50% chance of falling to the left or to the right. The piles of balls are accumulated
9+
* in the slots at the bottom of the board.
10+
* Write a program that simulates the bean machine. Your program should prompt the
11+
* user to enter the number of the balls and the number of the slots in the machine.
12+
* Simulate the falling of each ball by printing its path. For example, the path for
13+
* the ball in Figure 7.13b is LLRRLLR and the path for the ball in Figure 7.13c is
14+
* RLRRLRR. Display the final buildup of the balls in the slots in a histogram. Here
15+
* is a sample run of the program:
16+
* (Hint: Create an array named slots. Each element in slots stores the number
17+
* of balls in a slot. Each ball falls into a slot via a path. The number of Rs in a path
18+
* is the position of the slot where the ball falls. For example, for the path LRLRLRR,
19+
* the ball falls into slots[4], and for the path RRLLLLL, the ball falls into
20+
* slots[2].)
21+
*
22+
* Enter the number of balls to drop: 5
23+
* Enter the number of slots in the bean machine: 8
24+
* LRLRLRR
25+
* RRLLLRR
26+
* LLRLLRR
27+
* RRLLLLL
28+
* LRLRRLR
29+
* O
30+
* O
31+
* OOO
32+
* Created by Sven on 08/29/19.
33+
*/
34+
package Chapter07;
35+
36+
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
37+
38+
import java.util.Scanner;
39+
40+
public class Exercise0737_Game_bean_machine {
41+
public static void main(String[] args) {
42+
Scanner input = new Scanner(System.in);
43+
System.out.print("Enter the number of balls to drop: ");
44+
int numOfBalls = input.nextInt();
45+
System.out.print("Enter the number of slots in the bean machine: ");
46+
int numOfSlots = input.nextInt();
47+
48+
int[] slots = new int[numOfSlots];
49+
for (int i = 0; i < numOfBalls; i++) {
50+
int rightCount = 0;
51+
for (int j = 0; j < numOfSlots - 1; j++) {
52+
boolean isRight = Math.random() > 0.5;
53+
System.out.print(isRight ? "R" : "L");
54+
if (isRight) {
55+
rightCount++;
56+
}
57+
}
58+
System.out.println();
59+
slots[rightCount]++;
60+
}
61+
System.out.println();
62+
63+
display(slots);
64+
}
65+
66+
public static void display(int[] slots) {
67+
int maxHeight = slots[0];
68+
for (int i = 0; i < slots.length; i++) {
69+
if (slots[i] > maxHeight) {
70+
maxHeight = slots[i];
71+
}
72+
}
73+
74+
for (int i = maxHeight; i > 0; i--) {
75+
for (int j = 0; j < slots.length; j++) {
76+
if (slots[j] == i) {
77+
System.out.print("0");
78+
slots[j]--;
79+
} else {
80+
System.out.print(" ");
81+
}
82+
}
83+
System.out.println();
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)