Puzzle grid recursion ( infinite recusion errors )...

This topic is 4316 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello all, I have run into a wall with my current project( been working on a solution the past couple of days ) dealing with recursion. I have researched tuts on the web, but they are either all to easy, or to complex. I am trying to search through a 2D array of structs, for like values ( of the structs members ). Basicly, I am trying to 'link' structs together based on shared values ( in this case colors ). I have written and re-written hack-code, but am looking for way to clean it up using controlled recursion... Any ideas? ahayweh

Share on other sites
If it's just a 2d array, it's probably simpler to express as an iteration. You have to traverse the array once for every element in it. So flatten the array and traverse it once for each element (but don't link structs to themselves!), in pseudo-python:

for struct1 in flat_struct_array:    for struct2 in flat_struct_array:        if (not struct1 == struct2) and shared_values(struct1, struct2):            link(struct1, struct2)

Share on other sites
I agree that a nested iteration is probably the best approach; any particular reason for your choice of recursion? Any chance you can post the structure definitions and maybe describe exactly how the linking process needs to work?

Share on other sites
Hey guys,

Thanks for the input. I switched to iterative processing, and was able to fix the problem. I guess I was looking to recursion as a more sophisticated way of solving it, but at this point, I just want to get through this block of code. Alright, thanks again.

Share on other sites
Depending on the number of objects and distinct classes, it might be best to do something more like this:
Hashtable<paramType,List<myObject>> stuff = new Hashtable();foreach(myObject obj in mainList){ List<myObject> list = stuff[obj.param]; if(list == null) list = stuff[obj.param] = new List<myObject>(); list.add(obj);}foreach(List<myObject> list in stuff){ // ... link all items in this list}

Share on other sites
Are you trying to link structs only for like colors in an area or across the whole pixel set?

eg: 2 blobs of blue seperate by green
> 3 lists
where two lists of blue blobs and the green
or
> 2 lists
where the blue blobs are connected in a single list + green list

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• Forum Statistics

• Total Topics
631782
• Total Posts
3002332
×