Esempio n. 1
0
main()
{
    int n, k;

    printf("Enter number of objects in the set (n)? ");
    n = GetInteger();
    printf("Enter number to be chosen (k)? ");
    k = GetInteger();
    printf("C(%d, %d) = %d\n", n, k, Combinations(n, k));
}
Esempio n. 2
0
void Drainer::GenerateMicros()
{
    m_Micros.reserve(static_cast<size_t>(m_Mgr.m_Solver.m_TotalStates));

    auto &sets = m_Mgr.m_Solver.m_BlockSets;
    std::vector<BlockSet> indexes;
    indexes.reserve(sets.size());
    for (auto &set : sets)
    {
        indexes.emplace_back();
        auto &lst = indexes.back();
        for (auto blk : set)
            lst.push_back(m_BlocksLookup[blk]);
    }
    std::vector<std::map<int, std::vector<std::map<int, BlockStatus>>>> dicc(sets.size());
    for(auto solution : m_Mgr.m_Solver.m_Solutions)
    {
        std::vector<std::vector<std::map<int, BlockStatus>>*> ddic;
        for (auto i = 0; i < sets.size(); i++)
        {
            auto m = solution.Dist[i];
            auto &lst = dicc[i][m];
            if (lst.empty())
            {
                std::vector<std::vector<BlockStatus>> dists;
                Combinations(sets[i].size(), m, dists);
                for(auto l : dists)
                {
                    lst.emplace_back();
                    auto &d = lst.back();
                    for (auto j = 0; j < sets[i].size(); ++j)
                        d.insert(std::make_pair(indexes[i][j], l[j]));
                }
            }
            ddic.push_back(&lst);
        }

        std::vector<int> stack;
        stack.reserve(sets.size());
        stack.push_back(0);
        while (true)
            if (stack.size() == sets.size())
                if (stack.back() < ddic[stack.size() - 1]->size())
                {
                    m_Micros.emplace_back();
                    auto &lst = m_Micros.back();
                    lst.resize(m_Blocks.size(), BlockStatus::Blank);
                    for (auto i = 0; i < stack.size(); ++i)
                        for (auto kvp : ddic[i]->at(stack[i]))
                            lst[kvp.first] = kvp.second;
#ifdef _DEBUG
                    for (auto row = 0; row < m_Mgr.m_Solver.m_Matrix.GetHeight(); ++row)
                    {
                        auto v = 0;
                        auto nr = m_Mgr.m_Solver.m_Matrix.GetRowHead(row).Right;
                        ASSERT(nr != nullptr);
                        while (nr->Col != m_Mgr.m_Solver.m_BlockSets.size())
                        {
                            for (auto blk : m_Mgr.m_Solver.m_BlockSets[nr->Col])
                                if (lst[m_BlocksLookup[blk]] == BlockStatus::Mine)
                                    ++v;
                            nr = nr->Right;
                            ASSERT(nr != nullptr);
                        }
                        ASSERT(nr->Value == v);
                    }
#endif

                    ++stack.back();
                }
                else
                {
                    stack.pop_back();
                    if (stack.empty())
                        break;
                    ++stack.back();
                }
            else if (stack.back() < ddic[stack.size() - 1]->size())
                stack.push_back(0);
            else
            {
                stack.pop_back();
                if (stack.empty())
                    break;
                ++stack.back();
            }
    }
}