程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 棋盤一馬,從定點走到指定位置,所有路徑

棋盤一馬,從定點走到指定位置,所有路徑

編輯:C#入門知識

[csharp] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
/************************************************************************/ 
/* 問題描敘:棋盤一馬,從定點走到指定位置,所有路徑                                                                     */ 
/************************************************************************/ 
namespace Chess_Horse 

    class Program 
    { 
        static void Main(string[] args) 
        { 
            Horse myHorse = new Horse(0, 0, 6, 7); 
            myHorse.Find(0, 0); 
            Console.ReadKey(); 
        } 
    } 
    /// <summary>  
    /// 定義一個棋盤類  
    /// </summary>  
    class ChessBoard 
    { 
        private Int32 Row; 
        private Int32 Column; 
        private Int32[,] MyChessBoard; 
        private Int32 _Step; 
        public ChessBoard(Int32 row, Int32 column) 
        { 
            Row = row; 
            Column = column; 
            MyChessBoard = new Int32[Row,Column]; 
            for(int i = 0; i < Row; i++) 
                for(int j = 0; j < Column; j++) 
                    MyChessBoard[i,j] = 0; 
            _Step = 0; 
        } 
 
        public override string ToString() 
        { 
            Console.WriteLine("棋盤的行數:{0},列數:{1}", Row, Column); 
            return base.ToString(); 
        } 
 
        public void SetChess(Int32 x, Int32 y, Int32 step) 
        { 
            MyChessBoard[y, x] = step; 
            _Step = step; 
        } 
 
        public Int32 GetChess(Int32 x, Int32 y) 
        { 
            return MyChessBoard[y, x]; 
        } 
 
        public Int32 ChessBoardRow 
        { 
            get 
            { 
                return Row; 
            } 
        } 
 
        public Int32 ChessBoardColumn 
        { 
            get 
            { 
                return Column; 
            } 
        } 
 
        public Int32 Step 
        { 
            get 
            { 
                return _Step; 
            } 
        } 
 
        public void DisplayChessBoardInformation() 
        { 
            Console.WriteLine("此路徑走了{0}步", _Step); 
            for (int i = 0; i < Row; i++) 
            { 
                for (int j = 0; j < Column; j++) 
                { 
                    Console.Write("{0, -3} ", MyChessBoard[i, j]); 
                } 
                Console.WriteLine(); 
            } 
            Console.ReadKey(); 
        } 
 
        public void Clear() 
        { 
            for (int i = 0; i < Row; i++) 
                for (int j = 0; j < Column; j++) 
                    MyChessBoard[i, j] = 0; 
        } 
    } 
 
    class Horse 
    { 
        private ChessBoard ChessBoard; 
        // 記錄馬可走的8個方向  
        private Int32[,] OffsetArray; 
         
        private Int32 Start_X; 
        private Int32 Start_Y; 
        private Int32 End_X; 
        private Int32 End_Y; 
        private Int32 Step; 
      
        private void InitOffsetArray() 
        { 
            OffsetArray = new Int32[2, 8]; 
            OffsetArray[0, 0] = 1; OffsetArray[1, 0] = 2; 
            OffsetArray[0, 1] = 2; OffsetArray[1, 1] = 1; 
            OffsetArray[0, 2] = 2; OffsetArray[1, 2] = -1; 
            OffsetArray[0, 3] = 1; OffsetArray[1, 3] = -2; 
            OffsetArray[0, 4] = -1; OffsetArray[1, 4] = -2; 
            OffsetArray[0, 5] = -2; OffsetArray[1, 5] = -1; 
            OffsetArray[0, 6] = -2; OffsetArray[1, 6] = 1; 
            OffsetArray[0, 7] = -1; OffsetArray[1, 7] = 2; 
        } 
 
        public Horse(Int32 start_X, Int32 start_Y, Int32 end_X, Int32 end_Y) 
        { 
            Start_X = start_X;  
            Start_Y = start_Y; 
            End_X = end_X; End_Y = end_Y;       
            Step = 1; 
            ChessBoard = new ChessBoard(10, 9); 
            ChessBoard.SetChess(Start_X, Start_Y, Step); 
            InitOffsetArray(); 
   
        } 
 
        public Horse(Int32 chessBoardRow, Int32 chessBoardColumn, Int32 start_X, Int32 start_Y, Int32 end_X, Int32 end_Y) 
        { 
            Start_X = start_X;  
            Start_Y = start_Y; 
            End_X = end_X; End_Y = end_Y; 
            Step = 1; 
            ChessBoard = new ChessBoard(chessBoardRow, chessBoardColumn); 
            ChessBoard.SetChess(Start_X, Start_Y, Step); 
            InitOffsetArray(); 
        } 
 
        public void Find(Int32 start_X, Int32 start_Y) 
        { 
            Int32 newStart_X; 
            Int32 newStart_Y; 
 
            for (int i = 0; i < 8; i++) 
            { 
                newStart_X = start_X + OffsetArray[0, i]; 
                newStart_Y = start_Y + OffsetArray[1, i]; 
                if (IsCheckOK(newStart_X, newStart_Y)) 
                { 
                    Step++; 
                    ChessBoard.SetChess(newStart_X, newStart_Y, Step); 
                    if ((newStart_X == End_X) && (newStart_Y == End_Y)) 
                    { 
                        ChessBoard.DisplayChessBoardInformation(); 
                    } 
                    else 
                        Find(newStart_X, newStart_Y); 
 
                    ChessBoard.SetChess(newStart_X, newStart_Y, 0); 
                    Step--; 
                } 
 
            } 
        } 
 
        private Boolean IsCheckOK(Int32 x, Int32 y) 
        { 
            if (x < 0 || y < 0 || x > (ChessBoard.ChessBoardColumn-1) || y > (ChessBoard.ChessBoardRow-1)) 
            { 
                return false; 
            } 
            else 
                if (ChessBoard.GetChess(x, y) != 0) 
                    return false; 
            return true; 
        } 
 
 
    } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved