Could you first format your code?
Also, it should be pretty easy to just debug the code yourself. Why don’t you step through your code with a debugger and see what goes wrong?
Recently someone else posted a similar issue for this problem: Floodfill 2016 January Silver Problem 3. You must either use a stack or a queue to do the DFS/BFS, because recursion of depth 5000*5000 = 25e6 will crash.
Also your arrays are 5000 in size which means that you can only index on [0, 4999] inclusive, but you are checking if(r < 0 || r > 5000 || c < 0 || c > 5000).
Since you double FJ’s segment lengths, the array coordinates have to be in the range [−2N−1,2N+1], which you can transpose to [0,4N+3). Your 2D array should have dimensions 4003\times 4003.
To condense it further, you can calculate a bounding box with minX , maxX , minY , maxY. Make sure to increment maxX and maxY by 1 and decrement minX and minY by 1, so you get all appropriate regions. Since the sum of FJ’s step lengths is at most 2000, the maximum area you will floodfill on is 10002 \times10002 by AM-GM.
Good job! I had a similar idea in this thread. @infimum gave a great idea in post #3, so you can reference my thread for how I got AC using a stack-based DFS. The stack theoretically runs in time because the machine isn’t designed to recurse 16\cdot 10^6 deep, but it could allocate that much memory on a stack. I personally would bound the search and make adjustments like incrementing the maxs and decrementing mins, but note that using a stack for DFS or a queue for BFS is also an option.