I have this function and apparently it causes my program to crash:
long long todos(long long x,long long i) {
x ^= (1 << i);
long long aux = i - 1;
if(aux >= 0) x ^= (1 << aux);
aux = i - 4;
if(aux >= 0) x ^= (1 << aux);
aux = i + 1;
if(aux < 16) x ^= (1 << aux);
aux = i + 4;
if(aux < 16) x ^= (1 << aux);
return x;
}
What I don’t understand is why when I change all the ^= ( for &= ~( it runs perfectly fine (although the output I am getting is different). Is there any logical explanation for this behavior?
In case you need the entire code: http://ideone.com/Z7qoof
Looks like your
dp()function recurses very deeply. Consider that a call todp(3)can evaluate all 65536 possible bitboards in sequence with your^, while with your&~a call todp(k)will only evaluate bitboards numerically beforek. Notice that you’re filling inmatin order inmain(); if you only depend on bitboards numerically beforek, you won’t recurse very deeply.EDIT: As for fixing this problem, you might think of dynamic programming as being of shortest paths in a directed acyclic graph. The trouble is that you don’t have an acyclic graph here. You’re doing depth-first search to find shortest paths here, which doesn’t…work. Try replacing it with something like breadth-first search or Dijkstra’s algorithm.