I am solving the 8 queen problem using backtracking. When I compiled the following code in codechef IDE, it showed correct output, but still it shows runtime error.
#include <stdio.h>
#include <math.h>
int board[8][8] = { { 0 } };
int demi[8][8] = { { 0 } };
int queen(int a, int b, int c);
void mark(int a, int b);
int main()
{
int i;
int b = queen(3, 0, 0);
for (i = 0; i < 64; i++)
{
int x = board[i / 8][i % 8];
if (x == 1)
printf(" %d ", x);
else
{
printf(" 0 ");
}
if ((i + 1) % 8 == 0)
printf("\n");
}
}
int queen(int a, int b, int c)
{
int t;
if (c == 8)
return 1;
if (a < 0 || a > 7 || b < 0 || b > 7)
return 0;
if (!(board[a][b] == 0))
return 0;
for (t = 0; t < 64; t++)
{
demi[t / 8][t % 8] = board[t / 8][t % 8];
}
mark(a, b);
board[a][b] = 1;
/* for(t = 2; t<8; t++)
{
if(queen(a+(9-t), b+1, c+1))
return 1;
if(queen(a-t, b+1, c+1))
return 1;
}*/
if (queen(a + 7, b + 1, c + 1))
return 1;
if (queen(a + 6, b + 1, c + 1))
return 1;
if (queen(a + 5, b + 1, c + 1))
return 1;
if (queen(a + 4, b + 1, c + 1))
return 1;
if (queen(a + 3, b + 1, c + 1))
return 1;
if (queen(a + 2, b + 1, c + 1))
return 1;
if (queen(a - 2, b + 1, c + 1))
return 1;
if (queen(a - 3, b + 1, c + 1))
return 1;
if (queen(a - 4, b + 1, c + 1))
return 1;
if (queen(a - 5, b + 1, c + 1))
return 1;
if (queen(a - 6, b + 1, c + 1))
return 1;
if (queen(a - 7, b + 1, c + 1))
return 1;
board[a][b] = 0;
for (t = 0; t < 64; t++) {
board[t / 8][t % 8] = demi[t / 8][t % 8];
}
return 0;
}
void mark(int a, int b)
{
int i;
for (i = 0; i < 64; i++)
{
int row = i / 8;
int col = i % 8;
if (row == a || col == b && !(row == a && col == b))
board[row][col] = 2;
if (abs(row - a) == abs(col - b))
board[row][col] = 2;
}
}
Output :
Moreover, if I change the driver statement to "queen(0,0,0) or queen(1,0,0)
", The result is well correct upto 4-5 columns, but is full of 0s in the remaining ones.
Where am I going wrong?
just look at your main routine:
it is supposed to return 0 on successful completion, but you don't return anything: so even if your program does what you want, since your return code is undefined.
Not likely to be 0 in that case => seen as wrong execution by the engine executing it, which doesn't even try to go further into analysing program output.
Fix (even if C99 makes it default, compiling with old C89 could yield undefined results):
As several people commented, the
return 0
statement is not mandatory in C99. But I suppose that your on-line platform enforces strict C89 rules so you have to add it.