USACO Silver Build Gates

Problem Link: http://www.usaco.org/index.php?page=viewproblem2&cpid=596
My Code: https://pastebin.com/ad25iQj4
I am using a recursive flood fill algorithm. However, I keep getting segmentation faults (which I am assuming is a stack overflow, although I could be wrong).
My idea was have an array that keeps track of if the cell was visited or is a fence. I build the fence by moving my current coordinates two times, so I can just check the space in between to see if there is a fence.
I then keep track of the number of regions, and I just subtract one from that to get the answer.
Any help would be greatly appreciated :slight_smile:

Maybe add some debug prints? (Though be sure to use endl so the streams are constantly being flushed)

I printed the x and y coordinates every iteration, but it just randomly stops at a certain number.

I think your bounds are too small. 2000 N’s should (?) break your code.

But it breaks even for the sample case, which reaches no where near the bounds.

What’s the issue? It passes all the test cases. If it’s not working locally, see here: