Write a program to find the occurrences of a given word in a specified 2D matrix of characters

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 = {
                                 {'A', 'N', 'L', 'Y', 'S'}, 
                                 {'I', 'S', 'D', 'E', 'S'}, 
                                 {'I', 'G', 'N', 'D', 'E'}
                             };
            string testWord = "DES";
            ArrayList al = new ArrayList();
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    if (matrix[i, j].ToString() == testWord.Substring(0, 1))
                    {
                        al.Add(i.ToString() + " " + j.ToString());
                        CheckPattern(matrix, testWord, i, j, testWord.Substring(0, 1), al);
                        al.Clear();
                    }
                }
            }
            Console.WriteLine("Total number of pattern = " +  counter);
        }

        public static void CheckPattern(char[,] matrix, string testWord, int row, int col, string currentString, ArrayList al)
        {
            if (testWord == currentString)
            {
                for (int i = 0; i < al.Count; i++)
                {
                    Regex reg = new Regex(" ");
                    string[] tempStr = reg.Split(al[i].ToString());
                    Console.Write(matrix[int.Parse(tempStr[0]), int.Parse(tempStr[1])] + " - " + "[" + tempStr[0] + ", " + tempStr[1] + "] ");
                }
                Console.WriteLine();
                counter++;
                return;
            }
            //Check left
            if (col > 0 && matrix[row, col - 1] == testWord[al.Count] && !al.Contains(row.ToString() + " " + (col - 1).ToString()))
            {
                al.Add(row.ToString() + " " + (col - 1).ToString());
                CheckPattern(matrix, testWord, row, col - 1, currentString + testWord[al.Count - 1], al);
                al.Remove(row.ToString() + " " + (col - 1).ToString());
            }
            
            //Check top left
            if (col > 0 && row > 0 && matrix[row - 1, col - 1] == testWord[al.Count] && !al.Contains((row - 1).ToString() + " " + (col - 1).ToString()))
            {
                al.Add((row - 1).ToString() + " " + (col - 1).ToString());
                CheckPattern(matrix, testWord, row - 1, col - 1, currentString + testWord[al.Count - 1], al);
                al.Remove((row - 1).ToString() + " " + (col - 1).ToString());
            }

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

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

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

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

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

Output
=======
D - [1, 2] E - [1, 3] S - [0, 4]
D - [1, 2] E - [1, 3] S - [1, 4]
D - [2, 3] E - [1, 3] S - [0, 4]
D - [2, 3] E - [1, 3] S - [1, 4]
D - [2, 3] E - [2, 4] S - [1, 4]
Total number of pattern = 5
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