I'm trying to implement a problem where there are "pairs" of points that you can teleport between when walking in the positive x direction, and you have to find the number of such permutations of "pairs" that can get you stuck in an infinite loop (i.e you keep teleporting back and forth). I'm trying to solve this problem by first generating all such pairs using recursion and then simulating for each point whether in your journey, you teleport back to a point that you've already been to (visited[point] = true). However, I'm having a lot of trouble implementing this because we have to process the point that the current wormhole connects to and the next point on the same y-coordinate together.
I use a map to store the pairs, and the points for each y-coordinate (using a set), and I've created a check() function to help me check.
bool check() {
for (int i = 0; i < n; i++) {
int done = 1;
pair<int, int> curp = wormholes[i];
map<pair<int, int>, bool> visited;
for (int j = 0; j < n; i++) visited[wormholes[j]] = false;
visited[wormholes[i]] = true;
while (done < n) {
curp = pairs[curp];
if (visited[curp]) return true;
visited[curp] = true;
if (xpery[curp.s].find(curp.f) == xpery[curp.s].end()) return false;
done += 1;
curp = mp(*(++(xpery[curp.s].find(curp.f))), curp.s);
if (visited[curp]) return true;
visited[curp] = true;
done += 1;
}
}
return false;
}
I return true if you get stuck in an infinite loop and false if you don't, but I'm pretty sure this doesn't work. Is there a better way to implement this?