Reputation: 111
I observe a really weird behavior. I'm pretty sure there's a bug in my code, but Eclipse's behavior is so strange that I cannot figure anything out.
First, here's my code: http://pastebin.com/JCaW53GM.
I'm trying to solve Project Euler's problem 96, i.e., I'm trying to build a Sudoku solver using Knuth's Dancing Links. After a lot of preparation, this procedure is supposed to solve the problem:
private static void search(int k) {
System.out.println("start search");
if (masterHeader.right == masterHeader) {
return;
}
// choose column
Column c = new Column("");
int s = Integer.MAX_VALUE;
for (Column j = (Column) masterHeader.right; j != masterHeader;
j = (Column) j.right) {
if (j.size < s) {
c = j;
s = j.size;
}
}
// dancing links
c.cover();
for (Datum r = c.bottom; r != c; r = r.bottom) {
o[k] = r;
for (Datum j = r.right; j != r; j = r.right) {
j.column.cover();
}
System.out.println("before calling search(" + k + " + 1)");
search(k + 1);
r = o[k];
c = r.column;
for (Datum j = r.left; j != r; j = j.left) {
j.uncover();
}
}
c.uncover();
}
Whenever I run the program, I get the following output:
start search
before calling search(0 + 1)
start search
before calling search(1 + 1)
start search
before calling search(2 + 1)
start search
before calling search(3 + 1)
start search
before calling search(4 + 1)
start search
before calling search(5 + 1)
start search
before calling search(6 + 1)
start search
before calling search(7 + 1)
start search
before calling search(8 + 1)
start search
After that---nothing. There is no error, no overflow, Eclipse just continues running forever. I have no clue at all what this means. I'm pretty sure the fact that it stops at search(9) has something to do with the many nines in my code. But there is no error, and I'm at a loss.
Can anybody enlighten me? What does Eclipse want? What did I do wrong?
Edit: Gaaah! I completely overlooked that last "start search" of the output. That was what was confusing me. Guess I was just too tired... anyway, sorry to everyone for bothering you. Yes, it is clearly just a simple case of infinite loop. I hope I'll be able to debug that myself.
Upvotes: 1
Views: 357
Reputation: 106508
r != c;
is an expression that only makes sense if you're talking about the exact same instance of an object. Since both r
and c
are Datum
objects, it will not evaluate to true
unless they are the exact same instance.
What you probably want to do is define .equals
instead on the Datum
class. Then, you can call !r.equals(c)
instead.
You could also do with a rename of those variables, as row
and column
are far more straightforward than r
and c
.
Upvotes: 2