List of possible words of given length from a letter matrix

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        public static int counter = 0;
        static void Main(string[] args)
        {
            char[,] matrix = {
                                 {'F', 'X', 'I', 'E'}, 
                                 {'A', 'M', 'L', 'O'}, 
                                 {'E', 'W', 'B', 'X'},
                                 {'A', 'S', 'T', 'U'}
                             };
            int wordLength = 2;
            ArrayList al = new ArrayList();
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    al.Add(i.ToString() + " " + j.ToString());
                    CheckPattern(matrix, wordLength, i, j, matrix[i, j].ToString(), al);
                    al.Clear();
                }
            }
            Console.WriteLine("Total number of words printed = " + counter);
        }

        public static void CheckPattern(char[,] matrix, int wordLength, int row, int col, string currentString, ArrayList al)
        {
            if (currentString.Length == wordLength)
            {
                Console.WriteLine(currentString);
                counter++;
                return;
            }
            //Check left
            if (col > 0 && !al.Contains(row.ToString() + " " + (col - 1).ToString()))
            {
                al.Add(row.ToString() + " " + (col - 1).ToString());
                CheckPattern(matrix, wordLength, row, col - 1, currentString + matrix[row, col - 1], al);
                al.Remove(row.ToString() + " " + (col - 1).ToString());
            }
            
            //Check top left
            if (col > 0 && row > 0 && !al.Contains((row - 1).ToString() + " " + (col - 1).ToString()))
            {
                al.Add((row - 1).ToString() + " " + (col - 1).ToString());
                CheckPattern(matrix, wordLength, row - 1, col - 1, currentString + matrix[row - 1, col - 1], al);
                al.Remove((row - 1).ToString() + " " + (col - 1).ToString());
            }

            //Check top
            if (row > 0 && !al.Contains((row - 1).ToString() + " " + col.ToString()))
            {
                al.Add((row - 1).ToString() + " " + col.ToString());
                CheckPattern(matrix, wordLength, row - 1, col, currentString + matrix[row - 1, col], al);
                al.Remove((row - 1).ToString() + " " + col.ToString());
            }
            
            //Check top right
            if (row > 0 && col < matrix.GetLength(1) - 1 && !al.Contains((row - 1).ToString() + " " + (col + 1).ToString()))
            {
                al.Add((row - 1).ToString() + " " + (col + 1).ToString());
                CheckPattern(matrix, wordLength, row - 1, col + 1, currentString + matrix[row - 1, col + 1], al);
                al.Remove((row - 1).ToString() + " " + (col + 1).ToString());
            }

            //Check right
            if (col < matrix.GetLength(1) - 1 && !al.Contains(row.ToString() + " " + (col + 1).ToString()))
            {
                al.Add(row.ToString() + " " + (col + 1).ToString());
                CheckPattern(matrix, wordLength, row, col + 1, currentString + matrix[row, col + 1], al);
                al.Remove(row.ToString() + " " + (col + 1).ToString());
            }

            //Check bottom right
            if (row < matrix.GetLength(0) - 1 && col < matrix.GetLength(1) - 1 && !al.Contains((row + 1).ToString() + " " + (col + 1).ToString()))
            {
                al.Add((row + 1).ToString() + " " + (col + 1).ToString());
                CheckPattern(matrix, wordLength, row + 1, col + 1, currentString + matrix[row + 1, col + 1], al);
                al.Remove((row + 1).ToString() + " " + (col + 1).ToString());
            }

            //Check bottom
            if (row < matrix.GetLength(0) - 1 && !al.Contains((row + 1).ToString() + " " + col.ToString()))
            {
                al.Add((row + 1).ToString() + " " + col.ToString());
                CheckPattern(matrix, wordLength, row + 1, col, currentString + matrix[row + 1, col], al);
                al.Remove((row + 1).ToString() + " " + col.ToString());
            }

            //Check bottom left
            if (row < matrix.GetLength(0) - 1 && col > 0 && !al.Contains((row + 1).ToString() + " " + (col - 1).ToString()))
            {
                al.Add((row + 1).ToString() + " " + (col - 1).ToString());
                CheckPattern(matrix, wordLength, row + 1, col - 1, currentString + matrix[row + 1, col - 1], al);
                al.Remove((row + 1).ToString() + " " + (col - 1).ToString());
            }
        }
    }
}

Output
=======
FX
FM
FA
XF
XI
XL
XM
XA
IX
IE
IO
IL
IM
EI
EO
EL
AF
AX
AM
AW
AE
MA
MF
MX
MI
ML
MB
MW
ME
LM
LX
LI
LE
LO
LX
LB
LW
OL
OI
OE
OX
OB
EA
EM
EW
ES
EA
WE
WA
WM
WL
WB
WT
WS
WA
BW
BM
BL
BO
BX
BU
BT
BS
XB
XL
XO
XU
XT
AE
AW
AS
SA
SE
SW
SB
ST
TS
TW
TB
TX
TU
UT
UB
UX
Total number of words printed = 84
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s