Notice! You need to enable the flag -std=c++11 to compile the code!
X missionaries (傳教士) and Y cannibals (食人族) must cross a river using a boat which can carry at most two people, under the constraint that, for both banks, if there are missionaries present on the bank, they cannot be outnumbered by cannibals. If they were, the cannibals would eat the missionaries. The boat cannot cross the river by itself with no people on board. Initially, they are all on the left bank.
List all possible solutions to the given X and Y.
This is a Partial Judge problem.
We define a "State" as follows:
// A state contains five components: // The first two components denote the current numbers of // missionaries and cannibals at the left bank of the river. // The third and fourth components denote the current numbers // of missionaries and cannibals at the right bank. // The fifth component denotes the location of the boat: // 1 means "left bank" and -1 means "right bank". using State = vector<int>;
Basically, you need to implement five functions:
// the starting porint of your implementation void solve(); // extend to other possible states from a certain state set<State> extend(State s); // may use s[4] to indicate the direction of move State Go(State s, int missionary, int cannibal); // check the validity of a state bool valid(State s); // check if all people are at the right bank bool found(State s);
Notice that, if you don't want to follow the scheme we provide, you can just implement the sovle() function and make the others blank, e.g. bool found(State s) { }.
A single line containing two integers seperated by a space.
The first number is X denoting the number of missionaries. The second number is Y denoting the number of cannibals.
Actually, you don't need to worried about the input, we handle it for you.
List all possible solutions one by one. "done" denotes the end of a solution.
A solution contains several moves. A move is represented by a line with the format of:
(#M on the left bank, #C on the left bank)(#M on the right bank, #C on the right bank) [left/right]
M: missionary, C: cannibal, left/right: of the boat.
There is
Just like below,
Also, we've already handled the output functionality for you, so you don't have to worried about this, either.
You just need to add your solutions to the variable set<list<State>> _solutions and the order of addition doesn't matter.