(I am trying to think of ways to avoid creating giant matrices and applying graph theory.)

If you are trying to see if a specific 1 can be connected through a path of 1s to a specific 2, then I would start at the two to see if there are any 1s connected to it. It seems like you are essentially going to need a floodfill algorithm, slightly tweaked. What I mean is, if you were in Paint, and your matrix was a bunch of pixels where 0 = white, 1 = green, 2 = blue, then if you use the Fill tool to click on your 1 (green) to fill with blue, then your goal is to see if any colors around your 2 get changed. So what you could do is :

(Assume your matrix is in [A])

- Step 1 - Copy [A] to [B].
- Step 2 - Starting at your 1, convert it to a two.
- Step 3 - Convert any adjacent 1s to 2s.
- Step 4 - Scan the two matrices row by row or column by column until an element at matrix [A] is not the same as in [B].
- Convert any adjacent 1s to 2s.

- Step 5 - If at any point you come to a point that is adjacent to your 2 and it has been converted, then there is a path. Else, keep scanning until the end of the matrix.
- Step 6 - Goto Step 4

This is not a very fast technique, but at the cost of making another matrix (another 10x15 matrix is >1350 bytes) you can speed it up a little (or if you are willing to destroy [A]). All you do is whenever you come across two non-matching elements with no adjacent 1s, convert the corresponding value in [A] to a 2, so that they are the same in both matrices (that way your program doesn't keep trying to scan adjacent values everytime you pass it).

]]>I'm working on a small programme, and I need to check if an element in a dynamically created matrix is connected to another element trough a series of ones.

Bad explanation, I know, so take this example:

Matrix:

[0,0,0,0,0]

[0,1,1,1,0]

[0,1,0,0,0]

[0,2,0,0,0]

Here I need to see if the digit 1 at row 2, column 4 is "connected" to number 2 through the other ones.

If this happens (one 1 becomes a 0):

[0,0,0,0,0]

[0,1,1,1,0]

[0,**0**,0,0,0]

[0,2,0,0,0]

The 1 is no longer "connected" and I need to convert it to another number.

The problem is that I don't know how to check this, since the matrix will be quite large (10x15) and there wont be a single, simple path of 1s.

I hope I explained my problem in such a way that it is atleast sort of understandable, and I hope someone will be able to guide me in the right direction.

/cheers