Rather than just giving a code, explain what you have done and also explain what the user was doing wrong. Just giving them the correct code without any explanation is of no use as they may not understand it.
As far as my code is concerned I have replaced gc() in nil96’s code with cin which was causing the runtime error. Rest of it is same. So I think it won’t be a problem for nil96 to understand the difference.
You may ask nil96 to explain his approach or understand it yourself.
The reason for SIGSEGV is stack overflow. Recursive function olx has 5 parameters and 4 local variables, all of them are 64 bit and recursion can be quite deep. Merge x1, x2, x3 and x4 into one local variable, make r and c global and it should work.
I am sorry for not explaining the code.I will further apologize as my algorithm was wrong too.This was a question of Tree diameter which i have implemented wrong as simple traversal(wrong algo)(my moving left,right,up,down).I am getting TLE now and trying my best to Submit the problem.