'Dot Connecting' Program

Status
Not open for further replies.

TheEternalVortex

Ars Praefectus
3,094
Subscriptor
A friend asked me to make a program for him, which would be required to connect a group of dots with lines:

x-x-x
|_|_|
x-x-x
|_|_|
x-x-x

Where 'x's are dots, '_'s are spaces, and '-'s and '|'s are lines.

The 'rules' are:
- Lines may only connect adjacent dots.
- All the possible lines have to be drawn.
- The dots have to be connected with one continuous line. Although you can go over the same line twice. This means you can only start the line at one dot (any is fine), but then have to be able to finish the lines without stopping.

I for one don't really have any idea as how to go about doing this, so I thought some of you might be able to help?
 

Iconoplast

Ars Scholae Palatinae
1,040
Idea that came to mind:

Write a recursive function that steps through the possible directions from each node and goes back one in the call stack to try other paths if the solution it's trying is impossible. Some really messy pseudo-code:

code:<HR>
Code:
makepath(var path_in_progress)
{
	if no untried directions, return
	otherwise try another step on the path, makepath(path_in_progress)		// call self with up to date path
	if successful, return path_in_progress
}
 
Status
Not open for further replies.