vector<vector<int>> combinationSum(vector<int>& candidates, int target) { std::sort(candidates.begin(), candidates.end()); vector<vector<int>> results; vector<int> partial; canConstruct(candidates, partial, results, target, 0); return results; }
bool Square::construct(SquareType type) { dirty = true; CHECK(canConstruct(type)); if (--constructions[type.getId()] <= 0) { PSquare newSquare = PSquare(SquareFactory::get(type)); /* if (creature && !newSquare->canEnter(creature)) return false;*/ level->replaceSquare(position, std::move(newSquare)); return true; } else return false; }
void canConstruct(const vector<int>& c, vector<int>& partial, vector<vector<int>>& results, int target, int i) { // Can I construct a combination sum using c[i] ... c[end]? if (i >= c.size() || target < c[i]) { return; } for (int j = i; j < c.size() && target >= c[j]; ++j) { partial.emplace_back(c[j]); if (target == c[j]) { results.emplace_back(partial); } else { canConstruct(c, partial, results, target - c[j], j); } partial.pop_back(); } }
void CvCityAI::AI_chooseProduction(bool bInterruptWonders) { VALIDATE_OBJECT CvPlayerAI& kOwner = GET_PLAYER(getOwner()); CvCitySpecializationAI* pSpecializationAI = kOwner.GetCitySpecializationAI(); bool bBuildWonder = false; // See if this is the one AI city that is supposed to be building wonders if(pSpecializationAI->GetWonderBuildCity() == this) { // Is it still working on that wonder and we don't want to interrupt it? if(!bInterruptWonders) { const BuildingTypes eBuilding = getProductionBuilding(); CvBuildingEntry* pkBuilding = (eBuilding != NO_BUILDING)? GC.getBuildingInfo(eBuilding) : NULL; if(pkBuilding && kOwner.GetWonderProductionAI()->IsWonder(*pkBuilding)) { return; // Stay the course } } // So we're the wonder building city but it is not underway yet... // Has the designated wonder been poached by another civ? BuildingTypes eNextWonder = pSpecializationAI->GetNextWonderDesired(); if(!canConstruct(eNextWonder)) { // Reset city specialization kOwner.GetCitySpecializationAI()->SetSpecializationsDirty(SPECIALIZATION_UPDATE_WONDER_BUILT_BY_RIVAL); } else { // to prevent us from continuously locking into building wonders in one city when there are other high priority items to build int iFlavorWonder = kOwner.GetGrandStrategyAI()->GetPersonalityAndGrandStrategy((FlavorTypes)GC.getInfoTypeForString("FLAVOR_WONDER")); int iFlavorGP = kOwner.GetGrandStrategyAI()->GetPersonalityAndGrandStrategy((FlavorTypes)GC.getInfoTypeForString("FLAVOR_GREAT_PEOPLE")); int iFlavor = (iFlavorWonder > iFlavorGP ) ? iFlavorWonder : iFlavorGP; if (GC.getGame().getJonRandNum(11, "Random roll for whether to continue building wonders") <= iFlavor) bBuildWonder = true; } } if(bBuildWonder) { CvCityBuildable buildable; buildable.m_eBuildableType = CITY_BUILDABLE_BUILDING; buildable.m_iIndex = pSpecializationAI->GetNextWonderDesired(); buildable.m_iTurnsToConstruct = getProductionTurnsLeft((BuildingTypes)buildable.m_eBuildableType, 0); pushOrder(ORDER_CONSTRUCT, buildable.m_iIndex, -1, false, false, false, false); if(GC.getLogging() && GC.getAILogging()) { CvString playerName; FILogFile* pLog; CvString strBaseString; CvString strOutBuf; m_pCityStrategyAI->LogCityProduction(buildable, false); playerName = kOwner.getCivilizationShortDescription(); pLog = LOGFILEMGR.GetLog(kOwner.GetCitySpecializationAI()->GetLogFileName(playerName), FILogFile::kDontTimeStamp); strBaseString.Format("%03d, ", GC.getGame().getElapsedGameTurns()); strBaseString += playerName + ", "; strOutBuf.Format("%s, WONDER - Started %s, Turns: %d", getName().GetCString(), GC.getBuildingInfo((BuildingTypes)buildable.m_iIndex)->GetDescription(), buildable.m_iTurnsToConstruct); strBaseString += strOutBuf; pLog->Msg(strBaseString); } } else { m_pCityStrategyAI->ChooseProduction(false); AI_setChooseProductionDirty(false); } return; }