365. 水壶问题

https://leetcode-cn.com/problems/water-and-jug-problem/

BFS+hash
原贴讲解:https://leetcode-cn.com/problems/water-and-jug-problem/solution/tu-jie-bfs-c-jie-zhu-unordered_set-queue-shi-xian-/

介绍一下unordered_set
可以自定义Hash_function
hash.find("c")==hash.end()
如果c在hash table 里的值为0

code:

class Solution {
    pair<int, int> op(int type, const pair<int, int> &state, int x, int y) {
        switch(type) {
            case 0 : return make_pair(x, state.second);
            case 1 : return make_pair(state.first, y);
            case 2 : return make_pair(0, state.second);
            case 3 : return make_pair(state.first, 0);
            case 4 :{
                int move = min(state.first, y-state.second);
                return make_pair(state.first - move, state.second + move);
            }
            case 5 : {
                int move = min(x-state.first, state.second);
                return make_pair(state.first + move, state.second - move);
            }
        }
        return make_pair(0,0);
    }
    struct HashPair {
        size_t operator()(const pair<int, int> &key) const noexcept
	    {
		    return size_t(key.first)*100000007 + key.second;
	    }
    };
public:
    bool canMeasureWater(int x, int y, int z) {
        unordered_set<pair<int,int>, HashPair> mark;
        queue<pair<int,int>> q;
        q.push(make_pair(0,0));
        while(q.empty() == false) {
            auto f = q.front();
            q.pop();
            if(f.first + f.second == z) {
                return true;
            }
            for(int i = 0; i < 6; i++) {
                auto next = op(i, f, x, y);
                if(mark.find(next) != mark.end()) {
                    continue;
                }
                mark.insert(next);
                q.push(next);
            }
        }
        return false;
    }
};
posted @ 2020-03-21 20:22  aaaaassss  阅读(103)  评论(0编辑  收藏  举报