We help IT Professionals succeed at work.
Get Started

Select patterns on a n x n grid where the number of patterns = 1

rd707 asked
Last Modified: 2012-05-09
I have a C# program that creates a series of ever increasing n x n grids.

For each created grid, I create every possible permutation of filled cells in that grid.

For each pattern created by the filled cells, I only want to retain grids where there is a single pattern on the grid - i.e.:

where the number of filled cells = the size of the largest pattern

For the purposes of this program, I only consider shapes to include cells above, below, to the left or to the right of a filled cell - not diagonally

Currently, I'm finding the first cell in the grid (actually modelled in a one-dimensional array) and then calling the following routine recursively:

- Add the passed cell index to a linked list and increment the count
- If there is a filled cell to the left, right, above or below that is not in the list, call the routine with this cell index.

If the final count = the number of filled cells in the grid then retain the pattern, else discard

This doesn't strike me as terribly efficient - is there a better way?
Watch Question
This problem has been solved!
Unlock 1 Answer and 16 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE