Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewljohnson/66a16f4441fc776b5384a510f542ec1d to your computer and use it in GitHub Desktop.
Save andrewljohnson/66a16f4441fc776b5384a510f542ec1d to your computer and use it in GitHub Desktop.
/*
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