Count longest 'snake' of contiguous cells?

Status
Not open for further replies.

dropkickweasel

Board Regular
Joined
Feb 2, 2014
Messages
70
Hi,

I posted a similar question a couple of weeks ago, made a little progress (I think), but remain completely stumped as to how to solve my problem.

I have a 21x21 grid of 1s and 0s. (These can be changed if you have a solution that works with blanks/letters/whatever).
The 1s and 0s will be distributed in different cells at different times, though the size of the grid will remain the constant.
I would like to be able to run a macro that will count the longest contiguous run of 1s, allowing for orthogonal changes in direction.
Each cell may only be used once with the exception of a 'crossroads' which may be passed through both vertically and horizontally.

In the image below, I have highlighted in orange the longest run of 1s, which would lead to a count/sum of 52.
In yellow are contiguous runs which are not the longest, or 'offshoots' of the longest run which should not be counted as they are not on the 'path' that I'm trying to trace.

Route.png


With help from people on this forum I have been using the following code to count cells in each direction, but this doesn't work for the crossroads, or offshoots.
Equally, there is no way to know which order to run the directional codes for the correct solution to any given path.
As a result of this, running the FromLeft and equivalent FromRight code consecutively produces a value twice the length of a single horizontal run of 1s.

Code:
Sub FromLeft()

Dim rng As Range
Set rng = Range("Input")

Dim r As Long
Dim c As Long

For r = 1 To 21 Step 1
    For c = 1 To 21 Step 1

'If the cell, or the cell directly left is 0, then leave the cell value as it is
If rng.Cells(r, c).Value = 0 Or rng.Cells(r, c - 1).Value = 0 Then
    rng.Cells(r, c).Value = rng.Cells(r, c).Value
'otherwise, if the cell plus the cell left is > 1, then the cell value becomes the value of the cell left + 1
ElseIf rng.Cells(r, c - 1).Value + rng.Cells(r, c).Value > 1 Then
    rng.Cells(r, c).Value = rng.Cells(r, c - 1).Value + 1
    rng.Cells(r, c - 1).Value = 0
'otherwise, leave the cell value as it was
Else: rng.Cells(r, c).Value = rng.Cells(r, c).Value
End If

    Next c
Next r

End Sub

Any suggestions or advice before I give up on this project?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Status
Not open for further replies.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top