I am writing a program that reads in a potential Latin Square and tells whether or not it is a valid latin square or not. Now I am trying tell if the region selected is a contiguous one or not.
The potential Latin Square and the location of the region are read in at the same time. The region [0,1][0,2][1,1][1,2]
would be a valid region because it is contiguous; [0,0][0,2][1,1][1,2]
would not be contiguous or valid because [0,0]
can't be reached. How would I tell if they are contiguous?
A reasonable approach to this problem is a flood fill algorithm.
Basically, start from any point in your region, and build a set of locations in your region that are connected to the starting point by marking all its neighbors in the region and then marking all of their neighbors in the region that aren't already marked. When there's nothing new to mark, you've located the maximal contiguous region containing your starting point. If it's not the whole region, the region is not contiguous.