| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11495 | Missionaries and Cannibals - 6 points |
|
| 11968 | Swapsort_Extend |
|
Description
Notice! You need to enable the flag -std=c++11 to compile the code!
- In codeblocks, go to Settings->Compiler->check the option "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]"->OK
- In dev-c++, go to Tools->Compiler options->check "Add the following commands ..."->type in "-std=c++11"(without qoute) ->OK
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) { }.
Input
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.
Output
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,
(1, 1)(1, 1) right
(2, 1)(0, 1) left
(0, 1)(2, 1) right
(0, 2)(2, 0) left
(0, 0)(2, 2) right
done
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.
Sample Input Download
Sample Output Download
Partial Judge Code
11495.cppPartial Judge Header
11495.hTags
Discuss
Description
Given an integer sequence, say {6, 1, 3, 4, 5, 2}, can we sort the sequence by keeping swapping the first number with its two neighbors: the second number and the last number ?
For example, Swap the first number 6 and with the last number 2 to get {2, 1, 3, 4, 5, 6}, and then swap the first number 2 and the second number 1 to get {1, 2, 3, 4, 5, 6}, and we get the sequence sorted.
In function.h we define a class called SwapSort. A constructor and member functions show_solutions, solve, isSorted are implemented. Your task is to complete the implementation of extend function:
1. set<State> extend(State s);
A State is defined as using State = vector<int>;
For some State s, we may extend it by swapping the first number with the second one, or swapping the first number with the last one. Therefore, from State s we may generate two new states, and insert them into a set.
Note:
1. no duplicate states are allowed in a solution.
2. For OJ submission, you may choose c++ 11 as your compiler.
You need to implement the function in function.cpp
Input
An integer sequence, separated by spaces. End by EOF.
Output
The output will be generated by function.h