农民领袖
- 积分
- 11894
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-7-23
- 最后登录
- 1970-1-1
|
https://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=449947&extra=&page=1
个人实现代码:
- // 类似如下dfs(下面的code里pair不能作为memo的key,待改)
- bool dfs(pair<int, int> temp, pair<int, int> range, vector<pair<int, int>>& buttons, unordered_map<pair<int, int>, bool>& memo) {
- if (memo.count(temp))
- return memo[temp];
- if (range.first <= temp.first && temp.second <= range.second)
- return true;
- if (range.second < temp.second)
- return false;
- for (auto button : buttons) {
- if (dfs({button.first + temp.first, button.second + temp.second}, range, buttons, memo)) {
- memo.insert();
- return true;
- }
- }
- memo[temp] = false;
- return false;
- }
- bool isInCocaMachineRange(pair<int, int> range, vector<pair<int, int>>& buttons) {
- unordered_map<pair<int, int>, bool> memo;
- return dfs({0,0}, range, buttons, memo);
- }
- int main() {
- vector<pair<int, int>> button1 = {{100, 120}, {200, 240}, {400, 410}};
- cout << (isInCocaMachineRange({100, 110}, button1) ? "In" : "Not in") << endl; // false
- cout << (isInCocaMachineRange({90, 120}, button1) ? "In" : "Not in") << endl; // true
- cout << (isInCocaMachineRange({300, 360}, button1) ? "In" : "Not in") << endl; // true
- cout << (isInCocaMachineRange({310, 360}, button1) ? "In" : "Not in") << endl; // false
- cout << (isInCocaMachineRange({1, 999999999}, button1) ? "In" : "Not in") << endl; // true
- return 0;
- }
复制代码 |
|