zoukankan      html  css  js  c++  java
  • C++ 里面set存储结构体

    题目链接:https://leetcode-cn.com/problems/water-and-jug-problem/

    官方题解也超时了。。。

    主要是看看set里面存储结构体的方法吧

    using PII = pair<int, int>;
    
    class Solution {
    public:
        bool canMeasureWater(int x, int y, int z) {
            stack<PII> stk;
            stk.emplace(0, 0);
            auto hash_function = [](const PII& o) -> hash<int>() {return (o.first) ^ hash<int>()(o.second);};
            unordered_set<PII, decltype(hash_function)> seen(0, hash_function);
            while (!stk.empty()) {
                if (seen.count(stk.top())) {
                    stk.pop();
                    continue;
                }
                seen.emplace(stk.top());
                
                auto [remain_x, remain_y] = stk.top();
                stk.pop();
                if (remain_x == z || remain_y == z || remain_x + remain_y == z) {
                    return true;
                }
                // 把 X 壶灌满。
                stk.emplace(x, remain_y);
                // 把 Y 壶灌满。
                stk.emplace(remain_x, y);
                // 把 X 壶倒空。
                stk.emplace(0, remain_y);
                // 把 Y 壶倒空。
                stk.emplace(remain_x, 0);
                // 把 X 壶的水灌进 Y 壶,直至灌满或倒空。
                stk.emplace(remain_x - min(remain_x, y - remain_y), remain_y + min(remain_x, y - remain_y));
                // 把 Y 壶的水灌进 X 壶,直至灌满或倒空。
                stk.emplace(remain_x + min(remain_y, x - remain_x), remain_y - min(remain_y, x - remain_x));
            }
            return false;
        }
    };
  • 相关阅读:
    进制
    enum
    文件操作fstream
    文件读取 FILE
    static_cast、dynamic_cast、reinterpret_cast、和const_c
    std::max 错误
    boost 时间
    c++ new 和delete
    c++ static静态
    BOOST 之filesystem, path
  • 原文地址:https://www.cnblogs.com/qingjiuling/p/13094870.html
Copyright © 2011-2022 走看看