码迷,mamicode.com
首页 > 其他好文 > 详细

完全暴力-字谜题

时间:2019-06-17 10:43:36      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:new   end   ati   color   char   tostring   游戏   nbsp   app   

        #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循环一个一个判断。

技术图片

 

完全暴力-字谜题

标签:new   end   ati   color   char   tostring   游戏   nbsp   app   

原文地址:https://www.cnblogs.com/Icecoldless/p/11038101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!