[C#] 连连看递归搜索算法 →→→→→进入此内容的聊天室

来自 , 2021-01-22, 写在 C#, 查看 173 次.
URL http://www.code666.cn/view/ccb421d5
  1. public class LineCore
  2.    {
  3.        //整体宽度
  4.        public int width { get; set; }
  5.        //整体高度
  6.        public int height { get; set; }
  7.        //前次被点击的图片
  8.        public Position lastPicture { get; set; }
  9.        //当前被电击的图片
  10.        public Position currentPicture { get; set; }
  11.        // 图片矩阵,-1表示没有图片,初始化时,任意matrix[x,y]当x==0或y==0时,matrix[x,y]=-1
  12.        public int[,] matrix { get; set; }
  13.  
  14.        public bool testLine(Position current)
  15.        {
  16.            //上次连接成功后,第一次点击,
  17.            if (currentPicture == null)
  18.            {
  19.                currentPicture = current;
  20.                return false;
  21.            }
  22.            //连续点击了同一幅图片
  23.            if (currentPicture.x==current.x&&currentPicture.y==current.y)
  24.            {
  25.                return false;
  26.            }
  27.            //不是相同的图像,不能消除
  28.            if (current.picture != currentPicture.picture)
  29.            {
  30.                lastPicture = null;
  31.                currentPicture = current;
  32.                return false;
  33.            }
  34.            lastPicture = currentPicture;
  35.            currentPicture = current;
  36.            //记录连接路径
  37.            List<Position> paths = new List<Position>();
  38.            paths.Add(currentPicture);
  39.            bool finded=false;
  40.            //向东搜索
  41.            finded = _find(paths, 2, Direct.East, lastPicture);
  42.            if (finded)
  43.            {
  44.                return true;
  45.            }
  46.            //向南搜索
  47.            finded = _find(paths, 2, Direct.South, lastPicture);
  48.            if (finded)
  49.            {
  50.                return true;
  51.            }
  52.            //向西搜索
  53.            finded = _find(paths, 2, Direct.West, lastPicture);
  54.            if (finded)
  55.            {
  56.                return true;
  57.            }
  58.            //向北搜索
  59.            finded = _find(paths, 2, Direct.North, lastPicture);
  60.            if (finded)
  61.            {
  62.                return true;
  63.            }
  64.            //搜索失败
  65.            return false;
  66.        }
  67.        /***
  68.         * results-> 搜索路径
  69.         * chance-> 还剩有转弯的机会,最多2次
  70.         * direct-> 当前前进的方向
  71.         * target-> 搜索的目标
  72.         * **/
  73.        protected bool _find(List<Position> results, int chance, Direct direct, Position target)
  74.        {
  75.            //路径中不能为空
  76.            if (results.Count <= 0)
  77.            {
  78.                return false;
  79.            }
  80.            //取路径中最后一个
  81.            Position currentPos = results.ElementAt(results.Count - 1);
  82.            //构造下一个将要检测的路径
  83.            Position nextPos = new Position();
  84.            switch(direct){
  85.                case Direct.East:
  86.                    nextPos.x=currentPos.x+1;
  87.                    nextPos.y=currentPos.y;
  88.                    break;
  89.                    case Direct.West:
  90.                    nextPos.x=currentPos.x-1;
  91.                    nextPos.y=currentPos.y;
  92.                    break;
  93.                    case Direct.South:
  94.                    nextPos.x=currentPos.x;
  95.                    nextPos.y=currentPos.y+1;
  96.                    break;
  97.                    case Direct.North:
  98.                    nextPos.x=currentPos.x;
  99.                    nextPos.y=currentPos.y-1;
  100.                    break;
  101.            }
  102.            bool overFlow = false;
  103.            //检测是否已经走出边界
  104.            if (nextPos.x < 0 || nextPos.x >= width || nextPos.y < 0 || nextPos.y>=height)
  105.            {
  106.                overFlow = true;
  107.            }
  108.            if (overFlow)
  109.            {
  110.                return false;
  111.            }
  112.            //检测是否是目标
  113.            if (target.x == nextPos.x&&target.y==nextPos.y)
  114.            {
  115.                results.Add(nextPos);
  116.                return true;
  117.            }
  118.            //不是-1 说明该位置有图片,不能通过
  119.            if (matrix[nextPos.x, nextPos.y]!=-1)
  120.            {
  121.                return false;
  122.            }
  123.            //加入路径
  124.            results.Add(nextPos);
  125.            bool find = false;
  126.            //按照当前方向继续试探
  127.            find = _find(results, chance, direct, target);
  128.            if (find)
  129.            {
  130.                return true;
  131.            }
  132.            //转弯机会已经耗尽
  133.            if (chance <= 0)
  134.            {
  135.                //此路不通,将路径最后一个位置删除
  136.                results.RemoveAt(results.Count - 1);
  137.                return false;
  138.            }
  139.            //当前方向向东,还可以转弯向南,向北继续试探
  140.            if (Direct.East == direct)
  141.            {
  142.                find = _find(results, chance-1, Direct.North, target);
  143.                if (find)
  144.                {
  145.                    return true;
  146.                }
  147.                find = _find(results, chance-1, Direct.South, target);
  148.                if (find)
  149.                {
  150.                    return true;
  151.                }
  152.                results.RemoveAt(results.Count - 1);
  153.                return false;
  154.            }
  155.            //当前方向向南,还可以转弯向东,向西继续试探
  156.            else if (Direct.South == direct)
  157.            {
  158.                find = _find(results, chance-1, Direct.East, target);
  159.                if (find)
  160.                {
  161.                    return true;
  162.                }
  163.                find = _find(results, chance-1, Direct.West, target);
  164.                if (find)
  165.                {
  166.                    return true;
  167.                }
  168.                results.RemoveAt(results.Count - 1);
  169.                return false;
  170.            }
  171.            //当前方向向西,还可以转弯向南,向北继续试探
  172.            else if (Direct.West == direct)
  173.            {
  174.                find = _find(results, chance-1, Direct.North, target);
  175.                if (find)
  176.                {
  177.                    return true;
  178.                }
  179.                find = _find(results, chance-1, Direct.South, target);
  180.                if (find)
  181.                {
  182.                    return true;
  183.                }
  184.                results.RemoveAt(results.Count - 1);
  185.                return false;
  186.            }
  187.            //当前方向向北,还可以转弯向东,向西继续试探
  188.            else
  189.            {
  190.                find = _find(results, chance-1, Direct.East, target);
  191.                if (find)
  192.                {
  193.                    return true;
  194.                }
  195.                find = _find(results, chance-1, Direct.West, target);
  196.                if (find)
  197.                {
  198.                    return true;
  199.                }
  200.                results.RemoveAt(results.Count - 1);
  201.                return false;
  202.            }
  203.        }
  204.        //联通成功后,将两个位置设置成空
  205.        public void onSuccess()
  206.        {
  207.            this.matrix[this.lastPicture.x, this.lastPicture.y] = -1;
  208.            this.matrix[this.currentPicture.x, this.currentPicture.y] = -1;
  209.            this.currentPicture = null;
  210.            this.lastPicture = null;
  211.        }
  212.  
  213.  
  214.    }
  215.    
  216.    public class Position
  217.    {
  218.        public int x { get; set; }
  219.        public int y { get; set; }
  220.        //picture 表示不同图像,如果是-1,表示为空
  221.        public int picture { get; set; }
  222.    }
  223.  
  224.    public enum Direct : int
  225.    {
  226.        East = 0, South = 1, West = 2, North = 3
  227.    }
  228. //csharp/1148

回复 "连连看递归搜索算法"

这儿你可以回复上面这条便签

captcha