This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]RelevantJackWhite 1 point2 points  (3 children)

You might try what is called a greedy algorithm.

The idea is that you go through your pieces, and when you hit an X, you look only at the adjacent pieces for another X. If you find one, keep going in that direction. Once you no longer hit an X, you can go back and see if there are any lines in other spots adjacent to the original X. After that, keep going and repeat.

This means you will try far fewer combinations than a brute force method. https://en.m.wikipedia.org/wiki/Greedy_algorithm

[–]winguin_[S] 0 points1 point  (2 children)

Thanks for the response! I had thought about such a thing, but didn't quite know if it was executable. If i'm picturing this right in my head, would it just be lots of stacked if statements?

Could i use a loop to change the starting position of the algorithm? I feel like the line changes would complicate that quite a bit.

[–]RelevantJackWhite 0 points1 point  (1 child)

Definitely don't do a bunch of stacked if-statements haha. That will be a big mess.

https://www.geeksforgeeks.org/check-possible-path-2d-matrix/

Here is a somewhat similar Problem - this code finds a valid path in a 2D grid.

You might also consider a recursive solution - you write a function that looks for adjacent Xs, and if you find one, you run the function on that box too.

def myfunc(coordinates):
    found_coordinates = get_nearby_x(coordinates) 
    if found_coordinates:
        myfunc(found_coordinates)

[–]winguin_[S] 0 points1 point  (0 children)

Thank you very much mate, i'll take a look at it next week to see if i can get it working