voidgetTriangle(ifstream &fin, vector<vector<int>> &triangle){ int total; fin >> total;
int prev = 1; for (int i = 0; i < total; ++i) { vector<int> current; int cur = prev; while (cur--) { int item; fin >> item; current.emplace_back(item); } triangle.push_back(current); ++prev; } }
intfindMaxPath(vector<vector<int>> &triangle){ auto rows = (int)triangle.size(); if (!rows) return0; if (rows == 1) return triangle[0][0];
This is a search problem and the search space is the set of states of the bucket which can be encoded as a three dimensional array. The idea is to do an exhaustive search on all valid states by starting from the initial state, and this algorithm will at some point converge and reach a fixed point where all states are visited. Use the three dimensional array to store the state might be a little bit waste considering the state can be abstracted by the values in bucket B and C, so the state encoding can be optimized.
int capacity[3] = {0}; struct State { int water[3] = {0}; };
State pour(State state, int from, int to){ State next = state; int water = min(state.water[from], capacity[to] - state.water[to]); next.water[from] -= water; next.water[to] += water; return next; };
boolsatisfy(State &state){ if (state.water[0] == 0) returntrue; returnfalse; };