• 大小: 21KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-18
  • 语言: C#
  • 标签: C#  堆栈  回溯  

资源简介

1) 在国际象棋8×8棋盘中,马从任意初始位置按规则进行移动,要求每个方格只进入一次,求出走遍棋盘上全部64个方格的路径。 2) 求出马的行走路线后,对应每条路线可以输出一个类似棋盘的8×8的方阵,方阵中每个格的位置上显示的是此位置在整条路线上的步数(从1到64的整数)。 3) 用直观的界面演示寻找行走路线的回溯过程。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace CHTrace
{
    class CHTraceController
    {
        //马走的八个方向
        static Point[] _directions = new Point[]{new Point(21)new Point(2-1)new Point(-21)new Point(-2-1)
           new Point(12)  new Point(1-2)  new Point(-12)  new Point(-1-2)};

        //棋盘的8*8个位置
        Position[] Positions = new Position[8 8];

        //是否求所有解
        bool _isNeedAll;
        //是否优化
        bool _isOptimization;
        //所有解的路径
        List> _path = new List>();
        //求解的过程
        List _allPath = new List();

        int _allPathCount = 0;

        public int AllPathCount
        {
            get { return _allPathCount; }
            set { _allPathCount = value; }
        }

        internal List AllPath
        {
            get { return _allPath; }
            set { _allPath = value; }
        }

        internal List> Path
        {
            get { return _path; }
            set { _path = value; }
        }
 
        public CHTraceController(bool isNeedAll bool isOpmition)
        {
            _isOptimization = isOpmition;
            _isNeedAll = isNeedAll;
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    int weight = -8;//权值为出口的复数,初始为8
                    if (i == 0 || i == 7)
                    {
                        weight += 4;//如果在上下边,则少了4个出口
                    } else if (i == 1 || i == 6)
                    {
                        weight += 2;//如果在次上下边,则少了2个出口
                    }
                    if (j == 0 || j == 7)
                    {
                        if (i == 0 || i == 7)
                        {
                            weight += 2;//角点只有2个出口
                        } else if (i == 1 || i == 6)
                        {
                            weight += 3;//次角点只有5个出口
                        } else
                        {
                            weight += 4;
                        }
                    } else if (j == 1 || j == 6)
                    {
                        if (i == 0 || i == 7 || i == 1 || i == 6)
                        {
                            weight += 1;
                        } else
                            weight += 2;
                    }
                    Positions[i j] = new Position(i j 0 false weight);//设置初始权值和位置
                }
            }
        }

        //根据起点求各路径
        internal void Sovle(int startX int startY)
        {
            linkedStack curPath = new linkedStack();//当前路径
            linkedStack StorePositions = new linkedStack();//但回溯节点
            bool needFindNextPosition = true;//是否需要从新寻

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3226  2010-05-08 18:06  HorseOverChessBoard\HorseOverChessBoard\ChessHouseTrace.csproj

     文件      15710  2010-05-09 16:22  HorseOverChessBoard\HorseOverChessBoard\CHTraceController.cs

     文件        308  2010-05-04 21:42  HorseOverChessBoard\HorseOverChessBoard\Data\IStackDS.cs

     文件       2277  2010-05-09 15:00  HorseOverChessBoard\HorseOverChessBoard\Data\linkedStack.cs

     文件       1179  2010-05-04 21:42  HorseOverChessBoard\HorseOverChessBoard\Data\Node.cs

     文件        155  2010-05-04 21:51  HorseOverChessBoard\HorseOverChessBoard\Data\Path.cs

     文件       2184  2010-05-09 15:00  HorseOverChessBoard\HorseOverChessBoard\Data\Position.cs

     文件       1088  2010-05-09 15:00  HorseOverChessBoard\HorseOverChessBoard\Data\Step.cs

     文件       5899  2010-05-09 16:31  HorseOverChessBoard\HorseOverChessBoard\FmCHTrace.cs

     文件       6008  2010-05-09 16:31  HorseOverChessBoard\HorseOverChessBoard\FmCHTrace.resx

     文件      20054  2010-05-09 16:31  HorseOverChessBoard\HorseOverChessBoard\Form1.Designer.cs

     文件        260  2010-05-09 23:55  HorseOverChessBoard\HorseOverChessBoard\HorseOverChessBoard.gpState

     文件        919  2010-05-04 21:37  HorseOverChessBoard\HorseOverChessBoard\HorseOverChessBoard.sln

    ..A..H.     40448  2010-05-09 23:55  HorseOverChessBoard\HorseOverChessBoard\HorseOverChessBoard.suo

     文件        511  2010-05-04 21:42  HorseOverChessBoard\HorseOverChessBoard\Program.cs

     文件       1458  2009-04-02 16:53  HorseOverChessBoard\HorseOverChessBoard\Properties\AssemblyInfo.cs

     目录          0  2010-06-24 22:53  HorseOverChessBoard\HorseOverChessBoard\Data

     目录          0  2010-06-24 22:53  HorseOverChessBoard\HorseOverChessBoard\Properties

     目录          0  2010-06-24 22:53  HorseOverChessBoard\HorseOverChessBoard

     目录          0  2010-06-24 22:53  HorseOverChessBoard

----------- ---------  ---------- -----  ----

               101684                    20


评论

共有 条评论