|
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace LeetCode.Matrix |
| 10 | +{ |
| 11 | + [TestClass] |
| 12 | + public class WordSearchProblem |
| 13 | + { |
| 14 | + private int[] rowDir = { -1, -1, -1, 0, 0, 1, 1, 1}; |
| 15 | + private int[] colDir = { -1, 0, 1, -1, 1, -1, 0, 1 }; |
| 16 | + |
| 17 | + public bool WordSearchHelper(char[,] matrix, string word, int row, int col) |
| 18 | + { |
| 19 | + if (matrix[row, col] != word[0]) |
| 20 | + return false; |
| 21 | + |
| 22 | + for(int i=0; i < 8; i++) |
| 23 | + { |
| 24 | + int rowd = row + rowDir[i]; |
| 25 | + int cold = col + colDir[i]; |
| 26 | + int k = 1; |
| 27 | + |
| 28 | + for (; k < word.Length; k++) |
| 29 | + { |
| 30 | + // checking for boundaries. |
| 31 | + if (rowd < 0 || cold < 0 || rowd >= matrix.GetLength(0) || cold >= matrix.GetLength(1)) |
| 32 | + { |
| 33 | + break; |
| 34 | + } |
| 35 | + |
| 36 | + if (matrix[rowd,cold] != word[k]) |
| 37 | + { |
| 38 | + break; |
| 39 | + } |
| 40 | + |
| 41 | + // This is important, to make it go in specific direction (top or bottom or left or right or diagonal) |
| 42 | + rowd += rowDir[i]; |
| 43 | + cold += colDir[i]; |
| 44 | + } |
| 45 | + |
| 46 | + if ( k == word.Length) |
| 47 | + { |
| 48 | + return true; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + public void WordSearchSpecifcDirection(char[,] matrix, string word) |
| 56 | + { |
| 57 | + for( int i=0; i < matrix.GetLength(0); i++) |
| 58 | + { |
| 59 | + for (int j=0; j < matrix.GetLength(1); j++) |
| 60 | + { |
| 61 | + if (this.WordSearchHelper(matrix, word, i, j)) |
| 62 | + { |
| 63 | + Debug.WriteLine(string.Format("The word {0}: appeared at the index row: {1} col:{2}", word, i, j)); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public bool WordSearchZigZagHelper(char[,] matrix, string word, int row, int col, bool[,] visitedArray, int wordCount, string wordStr) |
| 70 | + { |
| 71 | + if (wordStr == word) |
| 72 | + { |
| 73 | + Debug.WriteLine(word); |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + visitedArray[row, col] = true; |
| 78 | + |
| 79 | + for (int i = 0; i < 8; i++) |
| 80 | + { |
| 81 | + int rowd = row + rowDir[i]; |
| 82 | + int cold = col + colDir[i]; |
| 83 | + |
| 84 | + if (!(rowd < 0 || cold < 0 || rowd >= matrix.GetLength(0) || cold >= matrix.GetLength(1)) // check for boundaries. |
| 85 | + && matrix[rowd, cold] == word[wordCount] |
| 86 | + && !visitedArray[rowd, cold]) |
| 87 | + { |
| 88 | + return this.WordSearchZigZagHelper(matrix, word, rowd, cold, visitedArray, wordCount + 1, wordStr + matrix[rowd, cold]); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + visitedArray[row, col] = false; |
| 93 | + |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + public bool Exist(char[,] board, string word) |
| 98 | + { |
| 99 | + bool[,] visitedArray = new bool[board.GetLength(0), board.GetLength(1)]; |
| 100 | + for(int i=0; i< board.GetLength(0); i++) |
| 101 | + { |
| 102 | + for (int j=0; j< board.GetLength(1); j++) |
| 103 | + { |
| 104 | + if (board[i,j] == word[0]) |
| 105 | + { |
| 106 | + bool result = this.WordSearchZigZagHelper(board, word, i, j, visitedArray, 1, word[0].ToString()); |
| 107 | + |
| 108 | + if (result) return true; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + [TestMethod] |
| 117 | + public void TestWordSearch() |
| 118 | + { |
| 119 | + char[,] matrix = { { 'c', 'a', 't', 'c', 'a', 't'}, |
| 120 | + { 'a', 'a', 'a', 'o', 'o', 's'}, |
| 121 | + { 'p', 'a', 't', 'w', 'a', 's'} |
| 122 | + }; |
| 123 | + |
| 124 | + // We can find words like cat, cap, tap, pat, was, tow, cow |
| 125 | + // We should also find the repetition, but dont go in zig-zag only 8 direction |
| 126 | + // (left, right, top, bottom, diagonal (rightdown), (rightup), (leftup), (leftdown) |
| 127 | + |
| 128 | + this.WordSearchSpecifcDirection(matrix, "tac"); |
| 129 | + } |
| 130 | + |
| 131 | + [TestMethod] |
| 132 | + public void TestWordSearchZigZag() |
| 133 | + { |
| 134 | + char[,] matrix = { { 'A', 'B', 'C', 'S'}, { 'S', 'F', 'C', 'S' }, { 'A', 'D', 'E', 'F' } }; |
| 135 | + |
| 136 | + Assert.IsTrue(this.Exist(matrix, "ABCCED")); |
| 137 | + Assert.IsTrue(this.Exist(matrix, "SFE")); |
| 138 | + Assert.IsFalse(this.Exist(matrix, "ABCB")); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments