線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2402
推到 Plurk!
推到 Facebook!

騎士問題 Knight

缺席
GGL
資深會員


發表:104
回覆:600
積分:335
註冊:2006-11-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-11-24 03:26:57 IP:211.76.xxx.xxx 未訂閱
這是source code typedef struct { int row; int col; int move; } record; int mx[8] = { -1, -2, -2, -1, 1, 2, 2, 1 }; int my[8] = { 2, 1, -1, -2, -2, -1, 1, 2 }; int **b; void PerformTour( int row, int column, int size ); void InitializeBoard( int boardSize ); void InputCorrect( int row, int col, int boardSize ); void InitializeBoard(int boardSize); int SpaceAvailable(int row, int col, int size, int moveType); int OnBoard(int row, int col, int size, int moveType); int CheckMoves(int &row, int &col, int size, int &count, record &r, stack &S, int &i); void PrintBoard(int boardSize); //--------------------------------------------------------------------------- void PerformTour(int row, int col, int boardSize) { int count = 1; //number of moves accomplished stack S; //Declares a stack record r; //Declares a record call r r.row = 0; r.col = 0; r.move = 0; int i = 0; b[row][col] = count; while(count < (boardSize*boardSize)) //loops till count reaches number of spots on board { if(CheckMoves(row, col, boardSize, count, r, S, i)) { ; } else { count--; if (count <= 0) { ShowMessage("無解"); break; } b[row][col] = -1; //decreases and reset everything to the previous position record temp; //pop the stack and start off were you left off temp = S.top(); S.pop(); row = temp.row; col = temp.col; i = temp.move; } } PrintBoard(boardSize); } //---------------------------------------------------------------------------
GGL
資深會員


發表:104
回覆:600
積分:335
註冊:2006-11-05

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-11-24 03:28:07 IP:211.76.xxx.xxx 未訂閱
//--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// // CheckMoves(int &, int&, int, int &, record &, stack &, int &) // Performs a for loop to check the possible moves that // could be made if the move is makeable it updates // and pushes the record ///////////////////////////////////////////////////////// int CheckMoves(int &row, int &col, int size, int &count, record &r,stack &S, int &i) { for( ;i < 8; i ) { if(OnBoard(row,col,size,i) && SpaceAvailable(row, col, size, i)) { r.row = row; r.col = col; r.move = i; //move the position to 1 more than the looping variable S.push(r); //pushes the new record on the stack row = mx[--i]; //sets the row and column to the new row and column values seting the counter back to one col = my[i]; b[row][col] = count; //places the number of moves in new table spot i = 0; //forces checking from move zero return 1; } } return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// // int OnBoard(int row, int col, int size, int MoveType) // Checks to see if the move would end up on the // board and returns 1 if it does. ///////////////////////////////////////////////////////// int OnBoard(int row, int col, int size, int MoveType) { if((((row mx[MoveType]) >= 0) && ((row mx[MoveType]) < size)) && (((col my[MoveType]) >= 0) && ((col my[MoveType]) < size))) return 1; else return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// // void InitializeBoard(int boardSize) // first it dynamically creates a board to the given // params then it fills the spaces with -1 to show // the cord is empty. ///////////////////////////////////////////////////////// void InitializeBoard(int boardSize) { for(int size = 0; size < boardSize; size ) b[size] = new int[boardSize]; for(int row = 0; row < boardSize; row ) for(int col = 0; col < boardSize; col ) b[row][col] = -1; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// // void InputCorrect(int row, int column, int boardSize // Check to see if the starting cords match the // board size given. ///////////////////////////////////////////////////////// void InputCorrect(int row, int col, int boardSize) { if(row >= boardSize || col >= boardSize) { //cerr << "Make sure the row and column are smaller than the" << //" board size\n\n"; ShowMessage("Make sure the row and column are smaller than the board size"); break; //exit(1); } }
GGL
資深會員


發表:104
回覆:600
積分:335
註冊:2006-11-05

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-11-24 03:29:09 IP:211.76.xxx.xxx 未訂閱
//--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// // void PrintBoard(int boardSize) // Prints the values of the cords in the shape of the // table. ///////////////////////////////////////////////////////// void PrintBoard(int boardSize) { int x=0,y=0; int row,col; for( row=0; row < boardSize; row ) { for( col=0; col < boardSize; col ) { if(b[row][col]==1) { TControlCanvas *PanelCanvas = new TControlCanvas; PanelCanvas->Control=Form1->suiImagePanel1; PanelCanvas->Brush->Color=clRed; PanelCanvas->TextOutA(8 col*25,8 row*25,b[row][col]); Form1->Refresh(); //TCanvas畫面重整 } else { TControlCanvas *PanelCanvas = new TControlCanvas; PanelCanvas->Control=Form1->suiImagePanel1; SetBkMode(PanelCanvas->Handle, TRANSPARENT); //去除文字背景色 PanelCanvas->TextOutA(8 col*25,8 row*25,b[row][col]); delete PanelCanvas; Form1->Refresh(); //TCanvas畫面重整 } } } } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// // int SpaceAvailable(int row, int col, int size, int MoveType // Checks after the move is on the board if the space // on the board is not empty returns 1 if empty and // 0 otherwise. ///////////////////////////////////////////////////////// int SpaceAvailable(int row, int col, int size, int MoveType) { row = mx[MoveType]; col = my[MoveType]; if(b[row][col] == -1) return 1; return 0; } //---------------------------------------------------------------------------
GGL
資深會員


發表:104
回覆:600
積分:335
註冊:2006-11-05

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-11-24 03:32:36 IP:211.76.xxx.xxx 未訂閱
這是我的執行畫面... 請問一下有何種方法可以加速執行的速度... 在啟始位置(0,0)的情況下,n<=7可以很快的求得答案,但是當n>=8的時候,run了很久還沒出現答案,請問有什麼比較好的演算法可以較快知道答案嗎?謝謝...
系統時間:2024-12-04 1:07:46
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!