一共有10层楼 每层有四个方向,假如东 南 西 北
从第一层走到第10层
例如
第一层 东 可以走到第二层
第一层 南 走回第一层
第一层 西 走到第二层
第一层 北 走到第三层=============
第二层 东 走回第一层
第二层 南 走回第一层
第二层 西 走回第一层
第二层 北 走回第一层====
那么第二层就是死路,所以要选走第一层的北,按照这个规则走到第10层c#算法,麻烦大家帮帮

解决方案 »

  1.   


        class Program
        {
            /// <summary>
            /// 动作
            /// </summary>
            public struct sstep
            {
                public int layer; //当前第几层
                public int direct;//方向 0 east 1 west 2 north 3 south 
            }
            /// <summary>
            /// 获取下一层 layer为当前层 direct为方向
            /// </summary>
            /// <param name="layer"></param>
            /// <param name="direct"></param>
            /// <returns></returns>
            static int getlayer(int layer, int direct) 
            {
                return 0;
            }
            static void Main(string[] args)
            {
                bool[,] directused=new bool[9,4]; //记录所有已过过的方向 当走过此门就置为true 如 directused[2][3]=true 表示 第三层 南入口已走过
                bool[] layerused =new bool[10];// 表示 到过的层数 如layerused[4]=true 表示第五层来过
                
                int currentladyer = 0;//当前所在的层
                bool issucess = false;//是否成功到达
                System.Collections.ArrayList steplist = new System.Collections.ArrayList();
                while (true) 
                {
                    bool iscontinue = false;//是否还继续走
                    //检查1-9层所有入口是否都走过了
                    for (int i = 0; i < 9; i++) 
                    {
                        if (directused[i, 0] == false || directused[i, 1] == false||directused[i, 2] == false||directused[i, 3] == false) 
                        {
                            iscontinue = true; 
                            break;
                        }
                    }
                    //开始遍历 以当前层 0 1 2 3入口遍历,得到的新层数为新的当前层
                    bool isendfind=false;//是否到达了第10层
                    if (directused[currentladyer, 0] && directused[currentladyer,1] && directused[currentladyer, 2] && directused[currentladyer, 3]) 
                    {
                        //如果当前层的所有入口都走过了 停止
                        break;
                    }
                    for (int i = 0; i < 4; i++) 
                    {
                        if (directused[currentladyer, i] == false) 
                        {
                            //以当前层,当前入口为参数 得到此入口进入哪一层
                            int newlayer = getlayer(currentladyer, i);
                            //如果下一层是第10层 成功到达
                            if(newlayer==9)
                            {
                                isendfind=true;
                                sstep step = new sstep();
                                step.direct = i;
                                step.layer = currentladyer;
                                steplist.Add(step);
                                break;
                            }
                            //查到到达的新层 是不是以前到达过的
                            if (layerused[newlayer] == true)
                            {
                                //新层是原来来过的,则将此入口标记已走过
                                directused[currentladyer, i] = true;
                            }
                            else
                            {
                                sstep step = new sstep();
                                step.direct = i;
                                step.layer = currentladyer;
                                steplist.Add(step);
                                directused[currentladyer, i] = true;
                                layerused[newlayer] = true;
                                currentladyer = newlayer;
                            }
                        }
                    }
                    if (isendfind)
                    {
                        issucess = true;
                        break;
                    }                           }
                if (issucess)
                {
                    //找到 路径
                    foreach (sstep step in steplist) 
                    {
                        Console.WriteLine("layer:" + step.layer.ToString() + " diretion:" + step.direct.ToString());
                    } 
                }
                else 
                {
                    //没有路径
                }        }
        }
      

  2.   

    getlayer 是你从已知数据中得到的 返回是目标楼层