dropkickweasel
Board Regular
- Joined
- Feb 2, 2014
- Messages
- 70
I posted a similar question a couple of days ago and am still struggling to come up with a solution to my problem.
I'm hoping if I rephrase it here someone might be able to help.
I have a 21x21 array in cells B2:V22.
Each cell in the array contains either a 0 or a 1. (If it makes any difference to potential help, these can easily be changed to blanks, letters, or whatever works for the most efficient solution).
I would like to calculate the length of the longest contiguous run of orthogonally adjacent 1s.
The longest run of contiguous 1s in the picture below is highlighted in green.
In light green is an alternate route that would still result in the longest run.
In blue are cells that are adjacent to the longest run, but not contributing to it.
In orange are other runs that are not the longest.
Using the formula:
In cell B24, I am able to produce the following result:
This formula checks to the left and above of the target cell, looking for 1s, and where there is a 1 in both the target cell and either the cell above or to the left, it adds 1 to the highest value found so far.
This allows me to run a cumulative total from the top left down towards the bottom right.
However, if I add a new IF to the formula to check to the right as well, then I get a circular reference error:
I understand why I get the circular reference error - the 5 to the right is generated from the 4 on the left, so adding the 5 to the 4 making a 9 on the left (which is what I want) would produce a 10 on the right (which I don't want), which would produce a 19 on the left etc. I just can't figure out a way to get around it.
So, can anyone offer any help with a solution for the longest run of orthogonally adjacent 1s?
I have looked into using a Lambda function as I believe they are able to deal with recursive situations like this, but I can't figure out how to write it with an appropriate way to exit the loop.
I'm open to all suggestions - helper cells, formulae and VBA - that would help me calculate a solution to this problem.
Additional considerations:
The area will always be a 21x21 array.
The longest path could have multiple changes of direction.
The longest path could start and end anywhere within the array.
I don't need to know where the longest path is, just how many cells it is.
Thanks in advance to anyone who is able to offer advice or a full-blown solution to this :]
I'm hoping if I rephrase it here someone might be able to help.
I have a 21x21 array in cells B2:V22.
Each cell in the array contains either a 0 or a 1. (If it makes any difference to potential help, these can easily be changed to blanks, letters, or whatever works for the most efficient solution).
I would like to calculate the length of the longest contiguous run of orthogonally adjacent 1s.
The longest run of contiguous 1s in the picture below is highlighted in green.
In light green is an alternate route that would still result in the longest run.
In blue are cells that are adjacent to the longest run, but not contributing to it.
In orange are other runs that are not the longest.
Using the formula:
Code:
=MAX(IF(SUM(A2,B2)>1,A24+1,B2),IF(SUM(B1,B2)>1,B23+1,B2))
This formula checks to the left and above of the target cell, looking for 1s, and where there is a 1 in both the target cell and either the cell above or to the left, it adds 1 to the highest value found so far.
This allows me to run a cumulative total from the top left down towards the bottom right.
However, if I add a new IF to the formula to check to the right as well, then I get a circular reference error:
I understand why I get the circular reference error - the 5 to the right is generated from the 4 on the left, so adding the 5 to the 4 making a 9 on the left (which is what I want) would produce a 10 on the right (which I don't want), which would produce a 19 on the left etc. I just can't figure out a way to get around it.
So, can anyone offer any help with a solution for the longest run of orthogonally adjacent 1s?
I have looked into using a Lambda function as I believe they are able to deal with recursive situations like this, but I can't figure out how to write it with an appropriate way to exit the loop.
I'm open to all suggestions - helper cells, formulae and VBA - that would help me calculate a solution to this problem.
Additional considerations:
The area will always be a 21x21 array.
The longest path could have multiple changes of direction.
The longest path could start and end anywhere within the array.
I don't need to know where the longest path is, just how many cells it is.
Thanks in advance to anyone who is able to offer advice or a full-blown solution to this :]