J
Jay Dee
I am trying to design a set of classes that calculate and return the
fastest root through a maze.
I have a class that represents a page containing text and a set of
links to other pages.
So far I have produces a class that will return the steps required to
navigate from the current page to the desired page but the process is
slow.
I understand that this is because my code is now where near optimised
and it keeps repeatedly checking the same paths over and over again.
I can not get my head around how to stop it though.
Douse anyone no of a solution or any pointers in the right direction.
Many thanks in advance for any support.
Jay Dee
public abstract class Page
{
// the collection of directly linked pages
public PageLinkList PageLinkList;
// the collection of all pages
public PageList PageList;
// uneque id of current page
public int PageNumber;
// activate seteps required to follow a direct link
public virtual void FollowLink(int linkIndex, ref Lines lines, Task
task);
// do the given lines match the current page
public abstract bool MatchLines(Lines lines);
// calculate the steps from the current page to the desired page
public List<int> StepsToPage(int pageNumber);
public List<int> StepsToPage(Page page);
private void StepsToPage(Page page, List<int> steps, int maxSteps)
{
if (this.PageNumber == page.PageNumber)
{
return;
}
if (maxSteps == 0)
{
steps.RemoveAt(steps.Count - 1);
return;
}
if (this.PageLinkList.Count == 0 || maxSteps <= 0)
{
return;
}
List<int>[] stepLists = new List<int>[this.PageLinkList.Count];
for (int i = 0; i < this.PageLinkList.Count; i++)
{
if (page.PageNumber == this.PageLinkList.LinkToPageNumber)
{
stepLists = new List<int>();
stepLists.Add(i);
}
else
{
stepLists = new List<int>();
stepLists.Add(i);
this.PageLinkList.LinkToPage.StepsToPage(page,
stepLists, maxSteps - 1);
}
}
// select the fastest path
List<int> fastestRoot = stepLists[0];
for (int i = 1; i < this.PageLinkList.Count; i++)
{
if (stepLists.Count > 0 && stepLists.Count <
fastestRoot.Count)
{
fastestRoot = stepLists;
}
}
// add the path to the return list
for (int i2 = 0; i2 < fastestRoot.Count; i2++)
{
steps.Add(fastestRoot[i2]);
}
}
}
fastest root through a maze.
I have a class that represents a page containing text and a set of
links to other pages.
So far I have produces a class that will return the steps required to
navigate from the current page to the desired page but the process is
slow.
I understand that this is because my code is now where near optimised
and it keeps repeatedly checking the same paths over and over again.
I can not get my head around how to stop it though.
Douse anyone no of a solution or any pointers in the right direction.
Many thanks in advance for any support.
Jay Dee
public abstract class Page
{
// the collection of directly linked pages
public PageLinkList PageLinkList;
// the collection of all pages
public PageList PageList;
// uneque id of current page
public int PageNumber;
// activate seteps required to follow a direct link
public virtual void FollowLink(int linkIndex, ref Lines lines, Task
task);
// do the given lines match the current page
public abstract bool MatchLines(Lines lines);
// calculate the steps from the current page to the desired page
public List<int> StepsToPage(int pageNumber);
public List<int> StepsToPage(Page page);
private void StepsToPage(Page page, List<int> steps, int maxSteps)
{
if (this.PageNumber == page.PageNumber)
{
return;
}
if (maxSteps == 0)
{
steps.RemoveAt(steps.Count - 1);
return;
}
if (this.PageLinkList.Count == 0 || maxSteps <= 0)
{
return;
}
List<int>[] stepLists = new List<int>[this.PageLinkList.Count];
for (int i = 0; i < this.PageLinkList.Count; i++)
{
if (page.PageNumber == this.PageLinkList.LinkToPageNumber)
{
stepLists = new List<int>();
stepLists.Add(i);
}
else
{
stepLists = new List<int>();
stepLists.Add(i);
this.PageLinkList.LinkToPage.StepsToPage(page,
stepLists, maxSteps - 1);
}
}
// select the fastest path
List<int> fastestRoot = stepLists[0];
for (int i = 1; i < this.PageLinkList.Count; i++)
{
if (stepLists.Count > 0 && stepLists.Count <
fastestRoot.Count)
{
fastestRoot = stepLists;
}
}
// add the path to the return list
for (int i2 = 0; i2 < fastestRoot.Count; i2++)
{
steps.Add(fastestRoot[i2]);
}
}
}