|
[Home] [Projects] [Delphi Techniques] [Math Topics] [Library] [Utilities] |
|
|
Problem DescriptionFind the largest and smallest paths through the number grid as you move from 'S' to 'F'. Paths can move straight or diagonally to the right but never to the left.
Source: Math and Logic Puzzles for PC Enthusiasts, J. J. Clessa, Dover Books. Background & TechniquesI believe this is our first excursion into graph searching here - but not our last. I know of no way to solve this problem without examining all paths and selecting those with the largest and smallest sums. Grids up to 13 by 13 are supported and solved in about a second. In simple terms, a graph is a set of points or numbers (called vertices) and some rules for connecting them (called edges). Mazes are graphs - each box is a vertex and the openings for next moves are edges. Many games can be treated as graphs where the game states (how the board looks at any point in the game) are vertices and the moves define the edges. The search technique implemented here is a "depth first" search which follows each path as far as possible before trying the next. In this case we can always proceed from S all the way to F, in other applications (for example maze solving) it may not be possible to get to the target vertex along any particular path. In other games and puzzles, it may be advantageous to use a "breadth first" search, where we explore all of the adjacent vertices and select the "best" one in order to arrive at a solution faster. Recursive calls to procedure GetPath are made, each call checking the vertices that are up and right, straight across right and down and right - the three possible paths from any vertex. A Stringgrid is used to display results. An option is provided using Stringrid's OnDrawCell event exit to animate the searching algorithm so you can watch the paths as they are tested. The board is a doubly dimensioned dynamic array of integers. It is sized with boardsize+2 elements each way so that we can keep an outline of 0's around the numbers in order to simplify testing when we reach an edge. If there is anything else unclear, send me an email. Addendum: There is now a Graph Searching page in Math Topics section Running/Exploring the ProgramSuggestions for further exploration
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2008, Gary Darby
All rights reserved.
|