#region 字謎遊戲 char[,] arr = { {'t','h','i','s'}, {'w','a','t','s' }, { 'o','a','h','g'}, { 'f','g','d','t'} }; HasWord(arr, "two"); HasWord(arr,"that"); HasWord(arr, "this"); HasWord(arr,"fat"); #endregion Console.ReadKey(); } static void HasWord(char[,] arr, string word) { StringBuilder sb = new StringBuilder(); if (word.Length > arr.GetLength(0) && word.Length > arr.GetLength(1)) return; for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { if (arr[i, j].Equals(word[0])) { sb.Clear(); sb.Append(arr[i, j]); if (j == 0) { #region 橫右 int count = j + 1; while (count <= word.Length + j) { if (count >= arr.GetLength(1)) break; sb.Append(arr[i, count]); count++; } if (sb.ToString().Equals(word)) { Console.WriteLine("找到:" + word); return; } #endregion sb.Remove(1, sb.Length - 1); #region 豎下 count = i + 1; while (sb.Length<word.Length) { if (count >= arr.GetLength(0)) break; sb.Append(arr[count,j]); count++; } if (sb.ToString().Equals(word)) { Console.WriteLine("找到:"+word); return; } #endregion sb.Remove(1, sb.Length - 1); #region 斜下右 int x=i+1, y=j+1; while (sb.Length<word.Length) { if (x >= arr.GetLength(0) || y >= arr.GetLength(1)) break; sb.Append(arr[x,y]); x++; y++; } if (sb.ToString().Equals(word)) { Console.WriteLine("找到:"+word); return; } #endregion sb.Remove(1, sb.Length - 1); #region 斜上右 x = i - 1; y = j + 1; while (sb.Length < word.Length) { if (j >= arr.GetLength(1) || x <0) break; sb.Append(arr[x, y]); x--; y++; } if (sb.ToString().Equals(word)) { Console.WriteLine("找到:" + word); return; } #endregion } else if (j == arr.GetLength(1) - 1) { #region 橫左 int count = j - 1; while (sb.Length < word.Length) { sb.Append(arr[i, count]); count--; if (count < 0) break; } if (sb.ToString().Equals(word)) { Console.WriteLine("找到" + word); return; } #endregion sb.Remove(1, sb.Length - 1); #region 豎下 count = i + 1; while (sb.Length < word.Length) { if (count >= arr.GetLength(0)) break; sb.Append(arr[count, j]); count++; } if (sb.ToString().Equals(word)) { Console.WriteLine("找到:" + word); return; } #endregion sb.Remove(1,sb.Length-1); #region 斜上左 int x = i - 1, y = j - 1; while (sb.Length<word.Length) { sb.Append(arr[x, y]); x--; y--; if (x <0|| y<0) break; } if (sb.ToString().Equals(word)) { Console.WriteLine("找到:" + word); return; } #endregion sb.Remove(1, sb.Length - 1); #region 斜下左 x = i + 1; y = j - 1; while (sb.Length < word.Length) { if (x >=arr.GetLength(0) || y <0) break; sb.Append(arr[x, y]); x++; y--; } if (sb.ToString().Equals(word)) { Console.WriteLine("找到:" + word); return; } #endregion } else { #region 中左 //左 if ((j + 1) >= word.Length) { int count = j - 1; while (sb.Length < word.Length) { sb.Append(arr[i, count]); count--; if (count < 0) break; } if (sb.ToString().Equals(word)) { Console.WriteLine("找到" + word); return; } } #endregion sb.Remove(1, sb.Length - 1); #region 中右 //右 if ((arr.GetLength(1) - j) >= word.Length) { int count = j + 1; while (count <= word.Length + j) { if (count >= arr.GetLength(1)) break; sb.Append(arr[i, count]); count++; } if (sb.ToString().Equals(word)) { Console.WriteLine("找到" + word); return; } } #endregion } } } } }
爲了找工做,不得不看數據結構與算法,剛學本身只能寫一些沒有任何結構,沒有任何思想的垃圾代碼了.算法
徹底for循環一個一個判斷。數據結構