The problem is here: B. 8 Queens, Again!!
I think I'm not getting the worst cases or missing something. My submission failed in test 2.
I've just checked rows, columns and diagonals for each input with their next ones.I thought this would be enough. Is there any other case where is can be valid or invalid? I'm not sure my code is all correct though.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int t, y[9], x[9], i, j, valid;
scanf("%d", &t);
while(t--)
{
valid = 1;
for(i = 0; i < 8; i++)
{
scanf(" %c %d", &x[i], &y[i]);
}
for(i = 0; i < 8; i++)
{
for(j = i+1; j < 8; j++)
{
if(x[i] == x[j] || y[i] == y[j] || abs(x[i] - x[j]) == abs(y[i]-y[j])) valid = 0;
}
}
(valid) ? printf("Valid\n") : printf("Invalid\n");
}
return 0;
}
You're scanf
ing the character of the coordinates in the input file into an int
.
for(i = 0; i < 8; i++)
{
char row;
scanf("%1c%1d ", &row, &y[i]);
x[i] = row - 'A';
}
An int
is probably 4 or 8 bytes. Since these are local, their contents are undefined until set. The character goes into the first byte, the remaining bytes are still undefined. It may work, it may not. Additionally, this will never work on big-endian machines, since you're storing it in the most-significant byte instead of least-significant. See: endianness
Make sure your argument types agree with the scanf
string. Possibly, your compiler gave you a warning to the effect of warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int *’
; if so, this is what it was trying to tell you.