Ejemplo n.º 1
0
        // 获取可能的下一步旋转
        private List <SolveNode> GetSolveNodeSon(SolveNode node)
        {
            List <SolveNode> nodeSon = new List <SolveNode>();
            int oppositeSpin;    // 获取该操作的相反旋转

            if (node.spin == -1) // 初始状态
            {
                oppositeSpin = -1;
            }
            else if (node.spin % 2 == 1)
            {
                oppositeSpin = node.spin - 1;
            }
            else
            {
                oppositeSpin = node.spin + 1;
            }
            // 本循环会成功执行五次,失败的一次为与传入操作的相反操作
            for (int currentSpin = 0; currentSpin < 6; currentSpin++)
            {
                if (currentSpin != oppositeSpin)
                {
                    SolveNode newNode = new SolveNode
                    {
                        path = new List <int>()
                    };
                    node.path.ForEach(i => newNode.path.Add(i));    // 拷贝步骤
                    newNode.path.Add(currentSpin);                  // 添加本步骤
                    newNode.spin        = currentSpin;              // 标记本步骤
                    int[,] currentRubik = CopyRubik(node.currentRubik);
                    OneStep(currentRubik, currentSpin);
                    newNode.currentRubik = currentRubik;
                    nodeSon.Add(newNode);                           // 加入
                }
            }
            return(nodeSon);
        }
Ejemplo n.º 2
0
        // 搜索魔方解
        public string Solve(bool boolExportStepImage, Canvas canvasRubik)
        {
            string stringPath = "";
            int    num        = 0;
            string filePath   = "";
            string stringTime = DateTime.Now.ToLocalTime().ToString("yyyyMMddhhmmss");

            if (boolExportStepImage)
            {
                FolderBrowserDialog m_Dialog = new FolderBrowserDialog();
                DialogResult        result   = m_Dialog.ShowDialog();

                if (result == DialogResult.Cancel)
                {
                    return("");
                }
                filePath = m_Dialog.SelectedPath.Trim();

                Directory.CreateDirectory($@"{filePath}\{stringTime}");
            }



            SolveNode finalNode;    // 最终节点
            // 初始化队列
            Queue <SolveNode> checkQueue    = new Queue <SolveNode>();
            SolveNode         solveNodeRoot = new SolveNode
            {
                spin         = -1,
                currentRubik = CopyRubik(rubik),
                path         = new List <int>()
            };

            checkQueue.Enqueue(solveNodeRoot);

            while (true)
            {
                if (CheckSurface(checkQueue.Peek().currentRubik))   // 检查队列头的魔方状态
                {
                    // 检查通过
                    finalNode = checkQueue.Peek();
                    break;
                }
                else
                {
                    // 检查不通过
                    foreach (SolveNode node in GetSolveNodeSon(checkQueue.Dequeue()))
                    {
                        checkQueue.Enqueue(node);
                    }
                }
            }

            if (boolExportStepImage)
            {
                SaveCanvas(canvasRubik, 96, $@"{filePath}\{stringTime}\Rubik_{num++}.png");
            }

            foreach (int step in finalNode.path)
            {
                string currentStep = OneStep(rubik, step);
                stringPath += currentStep;
                if (boolExportStepImage)
                {
                    UpdateRubikCanvas(canvasRubik);
                    SaveCanvas(canvasRubik, 96, $@"{filePath}\{stringTime}\Rubik_{num++}.png");
                }
            }

            return(stringPath);
        }