/    Sign up×
Community /Pin to ProfileBookmark

INTERESTING CHALLENGE: ‘Branched’ looping

I’m trying to increase of chances of getting into a certain reknowned English university to read Computer Science, so I’m trying to investigate different computational problems to add to my ‘portfolio’.

I was thinking of investigating ‘branched’ loops, in this case using a ‘maze generator’ as an example. Let’s say you have a grid of 10 x 10 (using multidimensional arrays), and a specified starting position for the maze generating function to begin, as well as a starting direction (i.e. N,S, E, W).

The easy part of the problem is fabricating an algorithm that will control what will happen at each square. The path might decide to continue in the same direction, turn left or right, and may even randomly branch out into two or even 3 directions. An appropriate type of square would then be displayed on the screen (e.g. with the bottom and left sides missing if the path was going East and then turned right) depending on the choices made.
The decision may depend on other possible factors, such as having to branch into an enclosed region (so as to avoid having any unused squares).

The hard part is making the function refer back TO ITSELF. Where the path of the maze branches off, the program must execute the same algorithm for the different paths in branches off into, and when those paths branch off more, again the same algorithm must be repeated. When the path comes to a dead end, that particular ‘branch’ must be terminated without disrupting other paths which may still be forming and branching.

How could such a task be achieved? If the function simply refers back to itself, there may be problems if a branch has to terminate, without stopping the function.
If you could give me any advice, it would be greatly appreciated, since this university placement (at Oxford) really means a lot to me. If you want to give yourself a challenge try out the task too!

Thanks,
Jamie Frost

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@VladdyMar 26.2003 — Let me start by saying that having function "refer" to itself is called recursive function execution and as long as the language supports it, there is nothing difficult about it.

I did solve a very similar problem (out of curiosity) about 10 years ago using C++. The code has been lost for awhile now, but here is a bit I rememeber.

The hard part is making sure there are no areas blocked by a wall and there are no areas without one (consider 4 neighboring squares - 2x2 - there should be at least one wall)
Copy linkTweet thisAlerts:
@JimJamJamminauthorMar 26.2003 — Q#1: How in that case would you terminate a 'branch' without terminating the whole function?

Q#2: Recognising 'enclosed' areas has always interested me, and I've never worked out how paint packages do it. How exactly can you determine if an area is enclosed? I do hope there are no fractal elements involved!
Copy linkTweet thisAlerts:
@JimJamJamminauthorMar 27.2003 — Great Scott! I actually managed to get it to work. However I started with a smaller project of being able to 'fill' areas, which just allowed me to test this 'recursive function execution' thingybob.

You can find at: [URL]http://www.tiffinmaths.co.uk/backgrounds/maze.htm[/URL]

Tell me what you think!

Just click on 'Add block' which you can use as a paint tool, or 'Fill' which will fill in an area. You can also select the colour tool. Thus the page works just as a simple paint package at the moment.

The code required to make the 'fill' function work was uncannily short, with minimum processing time. I don't know why I was worrying about having to 'terminate' a branch; you don't terminate the function, you just don't execute the branching again!

I know how to branch of into different paths now for my maze and I have the algorithm for finding an enclosed area - I just need to know how to apply this to work out what is an enclosed area and what isn't an enclosed area.

A path starting in the middle of the grid technically counts as an enclosed area, enclosed by the edges of the grid! However by not branching out at that point, it is not preventing squares from being reached, and thus it shouldn't be counted as an enclosed area.

Perhaps a solution is to compare the enclosed areas coming off from the square being processed in two or more different directions. If the enclosed areas have the same boundaries (i.e. the 2 paths are connected) then it does not necessarily have to branch off in that direction, if you get my drift.
Copy linkTweet thisAlerts:
@JimJamJamminauthorMar 30.2003 — OK, I know have the majority of the maze generator done now, and it even recognises enclosed areas. However I have discovered there is a problem with the main GLOBAL multi-dimensional array (which has '1' for an occupied square on the grid, and '0' for an empty square). If two branches are close to eachother, often one branch will move to occupy a square on the grid, but the other branch, if being processed immediately after, will not recognise that the recently occupied square is actually occupied. Thus you get paths which go through walls a stuff, which ruins the maze considerably.

Please help me with this problem since I am mystified!

The link is:

[URL]http://www.tiffinmaths.co.uk/backgrounds/mazegenerator.htm[/URL]

I made it so you can see the maze gradually being generated. It's quite interesting to watch!
Copy linkTweet thisAlerts:
@JimJamJamminauthorMar 30.2003 — I discovered what the problem is atleast. It has nothing to do with the main maze variable not being updated. The problem occur when two branches are turning into the same square. I'll just have to teach the maze a few rules so that it avoids these situations.
×

Success!

Help @JimJamJammin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...