dimanche 29 septembre 2019

Random path generation algorithm with defined path size

I'm trying to generate a path inside a matrix with a defined count of elements inside the picked path.

I can create from point A to point B with no problems using:

step(int[] s, int[] e) //start - end
{
   int d = delta(s, e);
   if (d > 1)
   {
      //spread around
      List<int[]> pts = new List<int[]>();
      pts.Add(new int[2] { s[0] -1, s[1]    }); //left
      pts.Add(new int[2] { s[0] +1, s[1]    }); //right
      pts.Add(new int[2] { s[0]   , s[1] +1 }); //top
      pts.Add(new int[2] { s[0]   , s[1] -1 }); //bot

      //remove out of bounds points
      List<int[]> goodPoints = new List<int[]>();
      foreach (var p in pts)
      {
         if (checkValidBoundries(p))
         {
            goodPoints.Add(p);
         }
      }

      //calculate lowest deltas
      int lowestDelta = int.MaxValue;
      int[] bestFit = new int[2];
      foreach (var p in goodPoints)
      {
         int localDelta = delta(p, e);
         if (localDelta == lowestDelta) //local shuffle
         {
            if (await coinFlip())
            {
               bestFit = p;
            }
         }
         else if (localDelta < lowestDelta)
         {
            lowestDelta = localDelta;
            bestFit = p;
         }
      }

      matrix.setValue(bestFit[0], bestFit[1]);
      step(bestFit, e);
   }
}

This code will recursive iterates until the path is over. This is the way I get the shortest path.

So my question is: How can I define a number of elements in the path?

For example: from A to B this algorithm gives me 6 elements, no matter the path it will always use the lowest delta between points. But if I want this path to be 7, 8 elements long?

I tried to pick one element at a time from the path and lock it so next run it will not consider it as valid, but it keeps going wrong.

The new path can be random no problem I just want to control the number of elements inside the "best path".

Any help? Thanks in Advance




Aucun commentaire:

Enregistrer un commentaire