Last active
July 6, 2022 19:29
-
-
Save andrewljohnson/66a16f4441fc776b5384a510f542ec1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Print out all ways for attackers to be blocked. blockAssignments maps ids of attackers to | |
vectors of defender ids for which blocks have already been chosen. defenders/defendersIterator specify | |
defenders that could block any of the attackers. This method iterates over all ways to choose blocks for | |
the remaining defenders. | |
example input: groupDefenders({0:[2 3] 1:[4 5]}, [6 7 8 9], 3) | |
output: | |
0: 2 3 | |
1: 4 5 | |
0: 2 3 9 | |
1: 4 5 | |
0: 2 3 | |
1: 4 5 9 | |
*/ | |
void groupDefenders(map<int, vector<int>> blockAssignments, const vector<int>& defenders, vector<int>::iterator defendersIterator) { | |
int x = 0; | |
for (auto const& x : blockAssignments) { | |
cout << x.first << ": "; | |
for (int defender:x.second) { | |
cout << defender << " "; | |
} | |
cout << "\n"; | |
} | |
if(defendersIterator == defenders.end()) { | |
return; | |
} | |
// choose all the possible ways that defenders[indexOfDefender] can be allocated, and print out each of them. | |
for (auto& attacker : blockAssignments) { | |
// push a defender into blockAssignments | |
attacker.second.push_back(*defendersIterator); | |
// recurse to print it and continue to push more defenders recursively | |
groupDefenders(blockAssignments, defenders, ++defendersIterator); | |
// pop the defender so it can be pushed to the next possible attacker in blockAssignments when looping | |
attacker.second.pop_back(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment