I need to extract the best path in terms of its length from a rectangular array like this array:
Quote:
|1||0||1||0|
|1||0||0||1|
|1||1||0||1|
|0||0||1||1|
The pathfinding rules:
1- Start from the indexes provided in the method signature where the rowIndex and colIndex are the positions of the starting point.
2- The ones must be able to connect with each other either horizontally, vertically, or diagonally.
3- You can not move to the cell that contains zero.
4- You can back by the path but not using the same cells i.e. you can move up the down the up but not using the same cells.
5- The best path from all the extracted paths (possible solutions) is longest one in terms of the connected ones by each other.
6- No problem if the value of the starting cell is 0.
I tried the following recursion algorithm, but it does not work for me and generate wrong output i.e. not as expected!!.
Here are the results:
Results
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using AUV_Topology;
using System.Collections.Generic;
using System.Media;
using System.Linq;
namespace AUVtopology
{
public partial class Form1 : Form
{
static int[,] array;
static List<int[]> path;
static Boolean containsArray(List<int[]> list, int[] array)
{
if (array == null || array.Length == 0)
{
return false;
}
foreach (var listArray in list)
{
if (listArray != null && listArray.Length == array.Length)
{
for (int i = 0; i < listArray.Length; i++)
{
if (i != listArray.Length - 1)
{
if (array[i] != listArray[i] && array[i + 1] != listArray[i + 1])
{
continue;
}
return true;
}
}
}
}
return false;
}
static List<int[]> getMaxPath(int[,] array, List<int[]> maxPath, int rowIndex, int colIndex)
{
maxPath.Add(new int[] { rowIndex, colIndex });
int rowLength = array.GetLength(0);
int colLength = array.GetLength(1);
if (maxPath.Count >= rowLength * colLength)
{
return maxPath;
}
colLength = colLength - 1;
rowLength = rowLength - 1;
List<int[]> futurePath;
if (colIndex < colLength && rowIndex < rowLength &&
array[rowIndex + 1, colIndex + 1] == 1 &&
!containsArray(maxPath, new int[] { rowIndex + 1, colIndex + 1 }))
{
maxPath = getMaxPath(array, maxPath, rowIndex + 1, colIndex + 1);
}
if (colIndex < colLength &&
array[rowIndex, colIndex + 1] == 1 &&
!containsArray(maxPath, new int[] { rowIndex, colIndex + 1 }))
{
futurePath = getMaxPath(array, maxPath, rowIndex, colIndex + 1);
if (futurePath.Count > maxPath.Count)
{
maxPath = futurePath;
}
}
if (rowIndex > 0 && colIndex < colLength &&
array[rowIndex - 1, colIndex + 1] == 1 &&
!containsArray(maxPath, new int[] { rowIndex - 1, colIndex + 1 }))
{
futurePath = getMaxPath(array, maxPath, rowIndex - 1, colIndex + 1);
if (futurePath.Count > maxPath.Count)
{
maxPath = futurePath;
}
}
if (rowIndex > 0 &&
array[rowIndex - 1, colIndex] == 1 &&
!containsArray(maxPath, new int[] { rowIndex - 1, colIndex }))
{
futurePath = getMaxPath(array, maxPath, rowIndex - 1, colIndex);
if (futurePath.Count > maxPath.Count)
{
maxPath = futurePath;
}
}
if (rowIndex > 0 && colIndex > 0 &&
array[rowIndex - 1, colIndex - 1] == 1 &&
!containsArray(maxPath, new int[] { rowIndex - 1, colIndex - 1 }))
{
futurePath = getMaxPath(array, maxPath, rowIndex - 1, colIndex - 1);
if (futurePath.Count > maxPath.Count)
{
maxPath = futurePath;
}
}
if (colIndex > 0 &&
array[rowIndex, colIndex - 1] == 1 &&
!containsArray(maxPath, new int[] { rowIndex, colIndex - 1 }))
{
futurePath = getMaxPath(array, maxPath, rowIndex, colIndex - 1);
if (futurePath.Count > maxPath.Count)
{
maxPath = futurePath;
}
}
if (colIndex > 0 && rowIndex < rowLength &&
array[rowIndex + 1, colIndex - 1] == 1 &&
!containsArray(maxPath, new int[] { rowIndex + 1, colIndex - 1 }))
{
futurePath = getMaxPath(array, maxPath, rowIndex + 1, colIndex - 1);
if (futurePath.Count > maxPath.Count)
{
maxPath = futurePath;
}
}
if (rowIndex < rowLength &&
array[rowIndex + 1, colIndex] == 1 &&
!containsArray(maxPath, new int[] { rowIndex + 1, colIndex }))
{
futurePath = getMaxPath(array, maxPath, rowIndex + 1, colIndex);
if (futurePath.Count > maxPath.Count)
{
maxPath = futurePath;
}
}
return maxPath;
}
So I thought in a different way!!. If we can generate a spanning tree for each node with one more thing; which is that you can back to a lower cell in terms of its indexes, if it is not listed as a previous parent. See the example:
See the image
The branch inside the circle with the blue color represents the longest path in terms of the number of cells with the value 1.
My question is how to implement this. How to create an array for each branch in order to compare them in terms of the length to obtain the longest one as the best solution ?.
What I have tried:
Recursion Function to extract the path as the rules, but I did not work for me as it made wrong results and with large array size it made a stack overflow!!.