• 大小: 25KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-17
  • 语言: Java
  • 标签: C#  

资源简介

骑士游历问题 C# 控制台程序 参考:数据结构java版 电子工业大学出版社

资源截图

代码片段和文件信息

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

namespace DemoHorseTravel
{
    class HorseTravel
    {
        private static int[] chessBoard;
        private bool show;

        public HorseTravel(int n int x int ybool show)
        {
            if (n < 5)
                MessageBox.Show(“设置的棋盘太小““请重新设置“);
            if (n > 8)
                MessageBox.Show(“设置的棋盘太大““请重新设置“);

            chessBoard =new int[nn];
            this.show=show;
            Start(xy);
        }
        //内部类,棋盘的一格坐标
        private class Position
        {
            private int x y;
            public int X
            {
                get
                {
                    return this.x;
                }
            }
            public int Y
            {
                get
                {
                    return this.y;
                }
            }
            public Position(int xint y)
            {
                if (x >= 0 && x < (chessBoard.GetUpperBound(0) + 1) && y >= 0 && y < (chessBoard.GetUpperBound(0) + 1))
                {
                    this.x = x;
                    this.y = y;
                }
                else
                    MessageBox.Show(“超过数组范围“);
            }
            public Position(Position p)
            {
                this.x = p.x;
                this.y = p.y;
            }
            public Position()
            {
                this.x = this.y = 0;
            }
        }
        //内部类结束
        private void Start(int x int y)
        {
            Position currentPosition = new Position(x y);
            int count = 1;                                  //统计第几步,count记录走过的格数
            int direction = 1;                              //direction表示8个方向
            int n = chessBoard.GetUpperBound(0) + 1;        //棋盘的大小
            while (count <= n * n && direction != 0)
            {
                chessBoard[currentPosition.X currentPosition.Y] = count;
                direction = select(currentPosition);
                if (direction != 0 && count < n * n)
                {
                    count++;
                    currentPosition = goStep(currentPosition direction);
                }
                else if (direction == 0 && count == 64)
                {
                    Console.WriteLine(“通过“);
                }
                else
                {
                    Console.WriteLine(“此路不通“);
                }  
            }

        }

        private int select(Position p)                      //为p位置选择应走的方向
        {
            //if (this.show)
            //{
            //    Console.WriteLine(“打印出当前棋盘“);
            //}
            int direction = 0;
            int minroad = 100;
            for (int i = 1; i <= 8; i++)
            {
                int road = 0;
                Position next1 = goStep(

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

     文件       6656  2010-07-16 19:20  DemoHorseTravel\bin\Debug\DemoHorseTravel.exe

     文件      17920  2010-07-16 19:20  DemoHorseTravel\bin\Debug\DemoHorseTravel.pdb

     文件      14328  2010-07-16 19:16  DemoHorseTravel\bin\Debug\DemoHorseTravel.vshost.exe

     文件        490  2007-07-21 01:33  DemoHorseTravel\bin\Debug\DemoHorseTravel.vshost.exe.manifest

     文件       2174  2010-07-16 17:29  DemoHorseTravel\DemoHorseTravel.csproj

     文件        919  2010-07-16 17:20  DemoHorseTravel\DemoHorseTravel.sln

    ..A..H.     10240  2010-07-16 19:23  DemoHorseTravel\DemoHorseTravel.suo

     文件        466  2010-07-16 19:17  DemoHorseTravel\obj\Debug\DemoHorseTravel.csproj.FileListAbsolute.txt

     文件       6656  2010-07-16 19:20  DemoHorseTravel\obj\Debug\DemoHorseTravel.exe

     文件      17920  2010-07-16 19:20  DemoHorseTravel\obj\Debug\DemoHorseTravel.pdb

     文件       6091  2010-07-16 19:20  DemoHorseTravel\Program.cs

     文件       1386  2010-07-16 17:19  DemoHorseTravel\Properties\AssemblyInfo.cs

     目录          0  2010-07-16 17:19  DemoHorseTravel\obj\Debug\TempPE

     目录          0  2010-07-16 17:21  DemoHorseTravel\bin\Debug

     目录          0  2010-07-16 19:20  DemoHorseTravel\obj\Debug

     目录          0  2010-07-16 17:19  DemoHorseTravel\bin

     目录          0  2010-07-16 17:19  DemoHorseTravel\obj

     目录          0  2010-07-16 17:19  DemoHorseTravel\Properties

     目录          0  2010-07-16 19:20  DemoHorseTravel

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

                85246                    19


评论

共有 条评论