-
Notifications
You must be signed in to change notification settings - Fork 0
/
riverCrossing.c
66 lines (51 loc) · 1.32 KB
/
riverCrossing.c
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
#include <stdio.h>
// [man, dog, sheep, vegetable]
char impassable[6] = {0b0011, 0b0110, 0b0111, 0b1000, 0b1001, 0b1100};
char stack[15];
int top = 0;
int isPassable(char status) {
// check repeating
for (int i = 0; i < top; i++)
if (stack[i] == status) return 0;
// check impassable
for (int i = 0; i < 6; i++)
if (impassable[i] == status) return 0;
return 1;
}
int moveCounter(char cur, char next) {
int result = 0;
// get diff
cur ^= next;
// count diff
while (cur > 0) {
if (cur & 1) result++;
cur >>= 1;
}
return result;
}
void river(char status) {
stack[top] = status;
top++;
// basecase is status to 0b0000
if (status == 0b0000) {
// print stack
for (int i = 0; i < top - 1; i++) printf("%d => ", stack[i]);
printf("%d\n", stack[top - 1]);
} else {
for (char i = 0; i <= 15; i++) {
// man on the boat toggle
if ((status & 0b1000) != (i & 0b1000)) {
// only allowed change less than or equal to 2 bit
if (moveCounter(status, i) <= 2) {
// not repeating and impassable
if (isPassable(i)) river(i);
}
}
}
}
top--;
}
int main() {
river(0b1111);
return 0;
}