Ide giving error


this is a code to check if number of islands is even or odd
this is the question:-

it doesnt work on hackerrank or codechef ide
segmentation fault but it works well on other ides.
it says there is a security breach

Hi @Krishna-Singh-2678805765519156,
in dis
for(int i=0;i<8;i++)
{
if(isSafe(x+dx[i],y+dy[i],n)&& grid[x+dx[i]][y+dy[i]]!=‘0’)
{
indices.push(make_pair(x+dx[i],y+dy[i]));
grid[x+dx[i]][y+dy[i]]=‘0’;
}
}
iterate i from 0 to 4.
since dx contain 4 elements ,dx[5],dx[6],etc must be giving segmentation fault since they havent been defined.
Hope dis will help.


this is the knight tours problem its working for all except when knight is at (0,0)position can you find why…

@Krishna-Singh-2678805765519156 In your BFS code you are running for loop from 0 to 7 when it should be from 0 to 3.
If this resolves your doubt mark it resolved.

there are 8 possible combinations for the knight to go…so it should run from 0 to 7 only can u xplain?

@Krishna-Singh-2678805765519156this solution is not optimised for optimised solution refer warnsdorffs-algorithm-knights-tour-problem
Your code is not giving answer for every position it will give answer for some suitable positions only, and 0,0 is not suitable for your code.
By changing the order of moves (xc and yc) you can make it suitable for 0,0 but it will become unsuited for other position.
to see how changing of moves effect run time, create a global variable of type long long and increment it at the beginning of function and then print it in the main.
You will see that this number is already in 10^6-7 so just a bit change in position will cause TLE.
If this resolves your doubt mark it as resolved.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.