FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Advanced-Programming-Spring-20/crossing.cpp at master · AnneEjsing/Advanced-Programming-Spring-20 · GitHub
AnneEjsing
/
Advanced-Programming-Spring-20
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Advanced-Programming-Spring-20
/
crossing.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
98 lines (89 loc) · 2.58 KB
Breadcrumbs
Advanced-Programming-Spring-20
/
crossing.cpp
Copy path
File metadata and controls
98 lines (89 loc) · 2.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
*
* Model for goat, cabbage and wolf puzzle.
* Author: Marius Mikucionis <marius@cs.aau.dk>
* Compile and run:
* g++ -std=c++17 -pedantic -Wall -DNDEBUG -O3 -o crossing crossing.cpp && ./crossing
*/
#
include
"
reachability.hpp
"
//
your header-only library solution
#
include
<
functional
>
//
std::function
#
include
<
list
>
#
include
<
array
>
#
include
<
iostream
>
enum
actor { cabbage, goat, wolf };
//
names of the actors
enum
class
pos_t
{ shore1, travel, shore2};
//
names of the actor positions
using
actors_t
= std::array<
pos_t
,
3
>;
//
positions of the actors
std::ostream&
operator
<<(std::ostream& stream,
actors_t
state)
{
for
(
auto
& position : state)
{
if
(position ==
pos_t
::shore1)
stream <<
"
1
"
;
if
(position ==
pos_t
::shore2)
stream <<
"
2
"
;
if
(position ==
pos_t
::travel)
stream <<
"
~
"
;
}
stream << std::endl;
return
stream;
}
auto
transitions
(
const
actors_t
& actors)
{
auto
res = std::list<std::function<
void
(
actors_t
&)>>{};
for
(
auto
i=
0u
; i<actors.
size
(); ++i)
switch
(actors[i]) {
case
pos_t
::shore1:
res.
push_back
([i](
actors_t
& actors){ actors[i] =
pos_t
::travel; });
break
;
case
pos_t
::travel:
res.
push_back
([i](
actors_t
& actors){ actors[i] =
pos_t
::shore1; });
res.
push_back
([i](
actors_t
& actors){ actors[i] =
pos_t
::shore2; });
break
;
case
pos_t
::shore2:
res.
push_back
([i](
actors_t
& actors){ actors[i] =
pos_t
::travel; });
break
;
}
return
res;
}
bool
is_valid
(
const
actors_t
& actors) {
//
only one passenger:
if
(
std::count
(
std::begin
(actors),
std::end
(actors),
pos_t
::travel)>
1
)
return
false
;
//
goat cannot be left alone with wolf, as wolf will eat the goat:
if
(actors[actor::goat]==actors[actor::wolf] && actors[actor::cabbage]==
pos_t
::travel)
return
false
;
//
goat cannot be left alone with cabbage, as goat will eat the cabbage:
if
(actors[actor::goat]==actors[actor::cabbage] && actors[actor::wolf]==
pos_t
::travel)
return
false
;
return
true
;
}
void
solve
(){
auto
state_space =
state_space_t
{
actors_t
{},
//
initial state
successors<
actors_t
>(transitions),
//
successor generator from your library
&is_valid};
//
invariant over all states
auto
solution = state_space.
check
(
[](
const
actors_t
& actors){
//
all actors should be on the shore2:
return
std::count
(
std::begin
(actors),
std::end
(actors),
pos_t
::shore2)==actors.
size
();
});
std::cout <<
"
# CGW
\n
"
;
unsigned
i =
0
;
for
(
auto
&& trace: solution)
std::cout << i++ <<
"
:
"
<< trace;
}
int
main
(){
solve
();
}
/*
* Sample output:
# CGW
0: 111
1: 1~1
2: 121
3: ~21
4: 221
5: 2~1
6: 211
7: 21~
8: 212
9: 2~2
10: 222
*/
Back
|
FazBrowse Home
|
New Git URL