Skip to content

Commit 03d8a2c

Browse files
committed
Finished chapter2 examples
1 parent cea1944 commit 03d8a2c

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

chapter2/generic_search.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class PriorityQueue {
190190

191191
pop() {
192192
let root = this.heap.shift();
193-
this.heap.unshift(this.heap[this.heap.length-1]);
193+
this.heap.unshift(this.heap[this.heap.length - 1]);
194194
this.heap.pop();
195195
this.heapify(0);
196196
return root;

chapter2/missionaries.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Missionaries and Cannibals</title>
5+
</head>
6+
<body>
7+
<pre>
8+
<script type="text/javascript" src="../util_web.js"></script>
9+
<script type="text/javascript" src="generic_search.js"></script>
10+
<script type="text/javascript" src="missionaries.js"></script>
11+
</pre>
12+
<p><a href="../index.html">Back to list</a></p>
13+
</body>
14+
</html>

chapter2/missionaries.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
if (typeof window === 'undefined') {
2+
gs = require(__dirname + '/generic_search.js');
3+
util = require(__dirname + '/../util.js');
4+
}
5+
6+
const MAX_NUM = 3;
7+
8+
class MCState {
9+
constructor(missionaries, cannibals, boat) {
10+
this.wm = missionaries; // west bank missionaries
11+
this.wc = cannibals; // west bank cannibals
12+
this.em = MAX_NUM - this.wm; // east bank missionaries
13+
this.ec = MAX_NUM - this.wc; // east bank cannibals
14+
this.boat = boat; // Boat on west bank?
15+
}
16+
17+
toString() {
18+
let result = "On the west bank there are " + this.wm + " missionaries ";
19+
result += "and " + this.wc + " cannibals.\n";
20+
result += "On the east bank there are " + this.em + " missionaries ";
21+
result += "and " + this.ec + " cannibals.\n";
22+
result += "The boat is on the " + (this.boat ? 'west' : 'east') + " bank.";
23+
return result;
24+
}
25+
26+
goalTest() {
27+
return this.isLegal() && this.em === MAX_NUM && this.ec === MAX_NUM;
28+
}
29+
30+
isLegal() {
31+
if (this.wm < this.wc && this.wm > 0) {
32+
return false;
33+
}
34+
if (this.em < this.ec && this.em > 0) {
35+
return false;
36+
}
37+
return true;
38+
}
39+
40+
successors() {
41+
let sucs = [];
42+
if (this.boat) { // boat on west bank
43+
if (this.wm > 1) {
44+
sucs.push(new MCState(this.wm - 2, this.wc, !this.boat));
45+
}
46+
if (this.wm > 0) {
47+
sucs.push(new MCState(this.wm - 1, this.wc, !this.boat));
48+
}
49+
if (this.wc > 1) {
50+
sucs.push(new MCState(this.wm, this.wc - 2, !this.boat));
51+
}
52+
if (this.wc > 0) {
53+
sucs.push(new MCState(this.wm, this.wc - 1, !this.boat));
54+
}
55+
if (this.wc > 0 && this.wm > 0) {
56+
sucs.push(new MCState(this.wm - 1, this.wc - 1, !this.boat));
57+
}
58+
} else { // boat on east bank
59+
if (this.em > 1) {
60+
sucs.push(new MCState(this.wm + 2, this.wc, !this.boat));
61+
}
62+
if (this.em > 0) {
63+
sucs.push(new MCState(this.wm + 1, this.wc, !this.boat));
64+
}
65+
if (this.ec > 1) {
66+
sucs.push(new MCState(this.wm, this.wc + 2, !this.boat));
67+
}
68+
if (this.ec > 0) {
69+
sucs.push(new MCState(this.wm, this.wc + 1, !this.boat));
70+
}
71+
if (this.ec > 0 && this.em > 0) {
72+
sucs.push(new MCState(this.wm + 1, this.wc + 1, !this.boat));
73+
}
74+
}
75+
return sucs.filter((x) => x.isLegal());
76+
}
77+
}
78+
79+
function displaySolution(path) {
80+
if (path.length == 0) { // sanity check
81+
return;
82+
}
83+
let oldState = path.shift();
84+
util.out(oldState.toString());
85+
for (currentState of path) {
86+
if (currentState.boat) {
87+
let mDiff = oldState.em - currentState.em;
88+
let cDiff = oldState.ec - currentState.ec;
89+
util.out(mDiff + " missionaries and " + cDiff + " cannibals moved from the east bank to the west bank.");
90+
} else {
91+
let mDiff = oldState.wm - currentState.wm;
92+
let cDiff = oldState.wc - currentState.wc;
93+
util.out(mDiff + " missionaries and " + cDiff + " cannibals moved from the west bank to the east bank.");
94+
}
95+
util.out(currentState.toString());
96+
oldState = currentState;
97+
}
98+
}
99+
100+
let start = new MCState(MAX_NUM, MAX_NUM, true);
101+
let solution = gs.bfs(start, (mc) => mc.goalTest(), (mc) => mc.successors());
102+
if (solution == null) {
103+
util.out("No solution found!");
104+
} else {
105+
let path = gs.nodeToPath(solution);
106+
displaySolution(path);
107+
}

0 commit comments

Comments
 (0)