Tuesday, August 26, 2014

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

public class Test
{
    public static void Main()
    {
        char[,] wordSearch = new char [3,3] { {'B', 'O', 'B'}, {'B', 'O', 'B'}, {'B', 'O', 'B'} };
        var wordList = new List<string>() { "BOB", "BOX", "COB" };
        printWordsInMaze(wordSearch, 3, 3, wordList);
    }
   
    public static void printWordsInMaze(char[,] wordSearch, int row, int col, List<string> wordList)
    {
        string[ ] directions = {"LR", "RL", "U", "D", "DUL", "DUR", "DDL","DDR"};
    for (int i = 0; i < col; i++)
    for (int j = 0; j < row; j++)
    {
        char letter = wordSearch[i,j];
          string pattern;
           List<string> candidates = wordList;
      
          int start = i;
          int end = j;
          foreach (var direction in directions)
          {
              int r = i;
              int c = j;
              pattern = String.Empty;
              switch (direction)
             {
                            case "LR":
                              while (c < col) { pattern += wordSearch[r,c]; c++;}
                              printMatchingWord(start, end, pattern, candidates, direction);
                            break;

                            case "RL":
                              while (c >= 0) { pattern+= wordSearch[r,c]; c--;}
                              printMatchingWord(start, end,pattern, candidates, direction);
                            break;

                            case "U":
                              while (r >= 0) { pattern += wordSearch[r,c]; r--;}
                              printMatchingWord(start, end,pattern, candidates, direction);
                            break;

                            case "D":
                              while (r < col) { pattern += wordSearch[r,c]; r++;}
                              printMatchingWord(start, end, pattern, candidates, direction);
                            break;

                            case "DUL" :
                              while (r >= 0 && c >= 0) { pattern += wordSearch[r,c]; r--; c--;}
                              printMatchingWord(start, end, pattern, candidates, direction);
                            break;

                            case "DUR" :
                              while (r >= 0 && c < col) { pattern += wordSearch[r,c]; r--; c++;}
                              printMatchingWord(start, end, pattern, candidates, direction);
                            break;

                            case "DDL" :
                              while (c >= 0 && r < col) { pattern += wordSearch[r,c]; c--; r++;}
                              printMatchingWord(start, end,pattern, candidates, direction);
                            break;

                            case "DDR" :
                              while (r < col && c < row) { pattern += wordSearch[r,c]; r++; c++;}
                              printMatchingWord(start, end, pattern, candidates, direction);
                            break;
               }
               //i = start;
               //j = end;
              }
         }
    }
   
private static void printMatchingWord(int i, int j, string pattern, List<string> candidates, string direction)
{
                                  foreach (var word in candidates)
                                  {
                                      Console.WriteLine("Pattern={0},word={1}", pattern, word);
                                       if (pattern.StartsWith(word))
                                        Console.WriteLine ("Word = {0} at row = {1} and col = {2} and direction = {3}", word, i,j, direction);
                                }
}

}

  • Success time: 0.06 memory: 33936 signal:0
    Pattern=BOB,word=BOB
    Word = BOB at row = 0 and col = 0 and direction = LR
    Pattern=BOB,word=BOX
    Pattern=BOB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BBB,word=BOB
    Pattern=BBB,word=BOX
    Pattern=BBB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BOB,word=BOB
    Word = BOB at row = 0 and col = 0 and direction = DDR
    Pattern=BOB,word=BOX
    Pattern=BOB,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=O,word=BOB
    Pattern=O,word=BOX
    Pattern=O,word=COB
    Pattern=OOO,word=BOB
    Pattern=OOO,word=BOX
    Pattern=OOO,word=COB
    Pattern=O,word=BOB
    Pattern=O,word=BOX
    Pattern=O,word=COB
    Pattern=O,word=BOB
    Pattern=O,word=BOX
    Pattern=O,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BOB,word=BOB
    Word = BOB at row = 0 and col = 2 and direction = RL
    Pattern=BOB,word=BOX
    Pattern=BOB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BBB,word=BOB
    Pattern=BBB,word=BOX
    Pattern=BBB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BOB,word=BOB
    Word = BOB at row = 0 and col = 2 and direction = DDL
    Pattern=BOB,word=BOX
    Pattern=BOB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BOB,word=BOB
    Word = BOB at row = 1 and col = 0 and direction = LR
    Pattern=BOB,word=BOX
    Pattern=BOB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BB,word=BOB
    Pattern=BB,word=BOX
    Pattern=BB,word=COB
    Pattern=BB,word=BOB
    Pattern=BB,word=BOX
    Pattern=BB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BO,word=BOB
    Pattern=BO,word=BOX
    Pattern=BO,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BO,word=BOB
    Pattern=BO,word=BOX
    Pattern=BO,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=OO,word=BOB
    Pattern=OO,word=BOX
    Pattern=OO,word=COB
    Pattern=OO,word=BOB
    Pattern=OO,word=BOX
    Pattern=OO,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BOB,word=BOB
    Word = BOB at row = 1 and col = 2 and direction = RL
    Pattern=BOB,word=BOX
    Pattern=BOB,word=COB
    Pattern=BB,word=BOB
    Pattern=BB,word=BOX
    Pattern=BB,word=COB
    Pattern=BB,word=BOB
    Pattern=BB,word=BOX
    Pattern=BB,word=COB
    Pattern=BO,word=BOB
    Pattern=BO,word=BOX
    Pattern=BO,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BO,word=BOB
    Pattern=BO,word=BOX
    Pattern=BO,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BOB,word=BOB
    Word = BOB at row = 2 and col = 0 and direction = LR
    Pattern=BOB,word=BOX
    Pattern=BOB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BBB,word=BOB
    Pattern=BBB,word=BOX
    Pattern=BBB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BOB,word=BOB
    Word = BOB at row = 2 and col = 0 and direction = DUR
    Pattern=BOB,word=BOX
    Pattern=BOB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=OOO,word=BOB
    Pattern=OOO,word=BOX
    Pattern=OOO,word=COB
    Pattern=O,word=BOB
    Pattern=O,word=BOX
    Pattern=O,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=OB,word=BOB
    Pattern=OB,word=BOX
    Pattern=OB,word=COB
    Pattern=O,word=BOB
    Pattern=O,word=BOX
    Pattern=O,word=COB
    Pattern=O,word=BOB
    Pattern=O,word=BOX
    Pattern=O,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BOB,word=BOB
    Word = BOB at row = 2 and col = 2 and direction = RL
    Pattern=BOB,word=BOX
    Pattern=BOB,word=COB
    Pattern=BBB,word=BOB
    Pattern=BBB,word=BOX
    Pattern=BBB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=BOB,word=BOB
    Word = BOB at row = 2 and col = 2 and direction = DUL
    Pattern=BOB,word=BOX
    Pattern=BOB,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    Pattern=B,word=BOB
    Pattern=B,word=BOX
    Pattern=B,word=COB
    

No comments:

Post a Comment