完全暴力-字谜题
Posted icecoldless
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完全暴力-字谜题相关的知识,希望对你有一定的参考价值。
#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循环一个一个判断。
以上是关于完全暴力-字谜题的主要内容,如果未能解决你的问题,请参考以下文章
精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解!(转载)