• 大小: 56KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-05
  • 语言: 其他
  • 标签:

资源简介

这个算法是用于解决所谓的骑士周游问题,里面用到了以前学过的贪心算法。程序是用C#写的,界面布局还算好吧,而且有动态的显示,看起来比较直观。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KnightTour
{
    class BackConque
    {
        public BackConque()
        {
            count = 0;
            isComplete = false;
            direction = 0;
        }

        #region 自定义属性
        
        private int[] vx = new int[] { 1 1 -1 -1 2 2 -2 -2 };
        private int[] vy = new int[] { 2 -2 2 -2 1 -1 1 -1 };
        private long count;   //比较次数 
        private int[] path;   //记录骑士周游的路径
        private int[] arrPath;  //记录周游的方向的路径
        private bool isComplete;  //判断是否结束
        private int direction ; //标记向哪个方向走

        #endregion
        
        //用来获取周游的路径
        public int[] Conque(int strx int stry int m int n)
        {
            strx--;   stry--;    //获得数组的下标
            //判断输入是否正确 
            if (!(( strx>=0 && strx < m) && (stry >=0&& stry < n)))
                return null;
            path = new int[m n];
            arrPath = new int[m n];
            //数据初始化
            int x = strx  y = stry length = 1;
            bool isFind;    //判断该趟走法是否有解
            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                {
                    arrPath[i j] = -1;
                    path[i j] = -1;
                }
            //初始化骑士起始点
            arrPath[x y] = 0;
            path[x y] = 1;
        ////////////////第一次迭代判断///////////////////
            while (length < (m * n)&&!isComplete  )
            {
                x = x + vx[direction];//
                y = y + vy[direction];//向前走一步
                length++;    count++;
                isFind = false;
                //判断该步是否越界
                if ((x>=0 && x < m)&& (y>=0 && y < n))             
                {   //判断该步是否已经走过
                    if (arrPath[x y] == -1)
                    {
                        arrPath[x y] = direction;
                        path[x y] = length;
                        direction = -1;
                        isFind = true;
                    }
                }
                if (!isFind)   //没有找到,则发生回溯,后退一步
                {
                    x = x - vx[direction];
                    y = y - vy[direction];
                    length--;
       /////////////////第二次迭代判断/////////////////////////////
                    while (direction == 7)
                    {   //一直回溯,直到该步还有可走的方向
                        count++;
                        //判断是否回溯到起始点
                        if (x == strx && y == stry)
                        {   //回到起始点,周游完成
                            isComplete = true;
                            break;
                        }
                        direction = arrPath[x y];
                        arrPath[x y] = -1;
                        x = x - vx[direction];
                        y = y - vy[direction];
                        length--;
         

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

     文件       5964  2011-12-27 08:54  KnightTour\KnightTour\Form1.cs

     文件       9364  2011-12-27 08:54  KnightTour\KnightTour\Form1.Designer.cs

     文件        491  2011-12-26 15:35  KnightTour\KnightTour\Program.cs

     文件       9053  2011-12-27 08:54  KnightTour\KnightTour\Form1.resx

     文件       3557  2011-12-27 09:28  KnightTour\KnightTour\BackConque.cs

     文件       6211  2011-12-27 09:42  KnightTour\KnightTour\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       2014  2011-12-27 08:54  KnightTour\KnightTour\obj\x86\Debug\KnightTour.Form1.resources

     文件        180  2011-12-26 15:58  KnightTour\KnightTour\obj\x86\Debug\KnightTour.Properties.Resources.resources

     文件        546  2011-12-27 08:54  KnightTour\KnightTour\obj\x86\Debug\GenerateResource.read.1.tlog

     文件        534  2011-12-27 08:54  KnightTour\KnightTour\obj\x86\Debug\GenerateResource.write.1.tlog

     文件      16896  2011-12-27 09:42  KnightTour\KnightTour\obj\x86\Debug\KnightTour.exe

     文件        688  2011-12-27 08:52  KnightTour\KnightTour\obj\x86\Debug\KnightTour.csproj.FileListAbsolute.txt

     文件       7897  2011-12-26 21:30  KnightTour\KnightTour\obj\x86\Debug\ResolveAssemblyReference.cache

     文件      32256  2011-12-27 09:42  KnightTour\KnightTour\obj\x86\Debug\KnightTour.pdb

     文件       4440  2011-12-27 08:52  KnightTour\KnightTour\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件        490  2010-03-17 22:39  KnightTour\KnightTour\bin\Debug\KnightTour.vshost.exe.manifest

     文件      11600  2011-12-27 08:52  KnightTour\KnightTour\bin\Debug\KnightTour.vshost.exe

     文件      16896  2011-12-27 09:42  KnightTour\KnightTour\bin\Debug\KnightTour.exe

     文件      32256  2011-12-27 09:42  KnightTour\KnightTour\bin\Debug\KnightTour.pdb

     文件       3720  2011-12-27 10:46  KnightTour\KnightTour\KnightTour.csproj

     文件       1376  2011-12-26 15:35  KnightTour\KnightTour\Properties\AssemblyInfo.cs

     文件       5612  2011-12-26 15:35  KnightTour\KnightTour\Properties\Resources.resx

     文件       2870  2011-12-26 15:35  KnightTour\KnightTour\Properties\Resources.Designer.cs

     文件        249  2011-12-26 15:35  KnightTour\KnightTour\Properties\Settings.settings

     文件       1095  2011-12-26 15:35  KnightTour\KnightTour\Properties\Settings.Designer.cs

     文件        872  2011-12-27 10:46  KnightTour\KnightTour.sln

    ..A..H.     19968  2011-12-27 10:46  KnightTour\KnightTour.suo

     目录          0  2011-12-26 15:35  KnightTour\KnightTour\obj\x86\Debug\TempPE

     目录          0  2011-12-26 15:35  KnightTour\KnightTour\obj\x86\Debug

     目录          0  2011-12-26 15:35  KnightTour\KnightTour\obj\x86

............此处省略9个文件信息

评论

共有 条评论

相关资源