| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Algorithms & Data Structures Discuss algorithms & data structures. Topics are not specific to any programming language. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Sequence of elements in a matrix
Hi,
I have a 3x3 matrix and I am trying to figure out whether there is a sequence of three similar elements diagonally, horizontally or vertically. For example: x x o o x o o o x Is there any algorithm dedicated on this kind of problem? |
|
#2
|
||||
|
||||
|
Re: Sequence of elements in a matrix
Algorithmically speaking, this is not such a difficult problem to handle that it would require some sort of special algorithm.
You can do this: I assume that the matrix M[3][3] is accessed by indices 0..2, the 1st index is the row index and the 2nd is the column index. 1. Check the type of element in the matrix center(M[1][1]) - let's say it's of type T (T being either x of o). 2. Now check if one of the following is true: Code:
a. (M[0][1] == T) AND (M[2][1] == T) //center top && center bottom elements
b. (M[1][0] == T) AND (M[1][2] == T) //center left&& center right elements
c. (M[0][0] == T) AND (M[2][2] == T) //top left && bottom right
d. (M[0][2] == T) AND (M[2][0] == T) //top right && bottom left
Regards, Zachm |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|