I'm currently working on this problem for my own practice. It's a paintball problem where I should determine if everyone can get a unique target. The problem is as follows I get a number of pair like:
- 1 2
- 3 4
- 3 2
- 4 1
If I get 1 2 it means that 1 can see 2 and therefore 1 can target 2. It goes the other way around to so 2 can see 1 and therefore 2 can target 1.
My assignment is to determine if everyone can get an unique target. If so I should output the numbers in order so first numbers 1 target than who number 2 should target and so on.
My implementation goes as follows:
- I read in the number of players and lines.
- For each line I create an edge.
- Then I create a bool array to keep track if a player has a target and another bool array to see if a player is a target.
- Then use a recursive method to find out the path which all player gets a target.
- I also have a graph class to keep track of all the edges.
I need advice on what I'm doing wrong or any tips on the implementation.
class Graph{
std::list<int> graph[1000];
public:
void addEdge(int from, int to){
graph[from].push_back(to);
}
std::list<int> getEdges(int e){
std::list<int> myList = graph[e];
return myList;
}
};
int n;
bool res = false;
int cnt = 0;
void checkPlayers(Graph graph,bool hasTarget[],bool isTarget[],int i,std::string myRes){
std::list<int> list = graph.getEdges(i);
std::list<int>::const_iterator iterator;
for(iterator = list.begin(); iterator != list.end(); ++iterator){
int currentplayer = (*iterator);
if(hasTarget[i] == false && isTarget[currentplayer] == false && currentplayer <= n){
hasTarget[i] = true;
isTarget[currentplayer] = true;
cnt++;
myRes += std::to_string(currentplayer);
checkPlayers(graph, hasTarget, isTarget,++i,myRes);
hasTarget[i] = false;
isTarget[currentplayer] = false;
cnt--;
//myRes.erase(myRes.begin()+ st)
}
}
if(cnt == n && res == false ){
res = true;
std::cout << myRes << std::endl;
}
}
int main(){
int m;
int playerOne, playerTwo;
Graph graph;
for(std::cin >> n >> m; m; --m){
std::cin >> playerOne >> playerTwo;
graph.addEdge(playerOne,playerTwo);
graph.addEdge(playerTwo,playerOne);
}
bool isTarget[1000] = {false};
bool hasTarget[1000] = {false};
checkPlayers(graph,hasTarget, isTarget,1,"");
}
I need advise on what im doing wrong
does this mean your code is not working as expected? In this case this Question is off-topic onCode Rewriew
. \$\endgroup\$