2015-10-09 74 views
0

對於騎士之旅的問題,我想出了這個答案;然而,它只是打印一個答案。我不知道如何打印所有答案。我知道我應該將查找巡視的輸出變爲無效,以避免完成,但我不知道如何。任何人都可以修改它?騎士之旅在C++中的所有答案

#include <iostream> 
using namespace std; 

const int ROW_COUNT = 6; 
const int COL_COUNT = 6; 
const int POSSIBLE_MOVES = 8; 

int row_delta[POSSIBLE_MOVES] = {2, 1, -1, -2, -2, -1, 1, 2}; 
int col_delta[POSSIBLE_MOVES] = {-1, -2, -2, -1, 1, 2, 2, 1}; 

int board[ROW_COUNT][COL_COUNT]; 

void print_board() { 
    for (int i = 0; i < ROW_COUNT; i++) { 
     for (int j = 0; j < COL_COUNT; j++) { 
      if (board[i][j] < 10) 
       cout << ' '; 
      cout << board[i][j] << ' '; 
     } 
     cout << endl; 
    } 
    cin.get(); 
} 

bool find_tour(int move_no, int current_row, int current_col) { 
    // uncomment the following two lines for debugging: 
    //cout << move_no << endl; 
    //print_board(); 

    if (move_no == ROW_COUNT * COL_COUNT) 
     return true; 

    for (int move = 0; move < POSSIBLE_MOVES; move++) { 
     int new_row = current_row + row_delta[move]; 
     int new_col = current_col + col_delta[move]; 

     if (new_row < 0 || new_row >= ROW_COUNT || new_col < 0 || new_col >= COL_COUNT) 
      continue; 

     if (board[new_row][new_col] != 0) 
      continue; 

     board[new_row][new_col] = move_no + 1; 
     if (find_tour(move_no + 1, new_row, new_col)) 
      return true; 
     board[new_row][new_col] = 0; 
    } 
    return false; 
} 

void solve(int init_row, int init_col) { 
    for (int row = 0; row < ROW_COUNT; row++) 
     for (int col = 0; col < COL_COUNT; col++) 
      board[row][col] = 0; 

    board[init_row][init_col] = 1; 
    if (find_tour(1, init_row, init_col)) 
     print_board(); 
    else 
     cout << "Failed to find a tour!\n"; 
} 

int main() { 
    solve(2, 3); 
} 
+0

看起來你可以使用一個循環。 *對於棋盤上的每個位置*,在給定位置解決。 –

+0

有關「C++騎士之旅」的更多示例,請參閱StackOverflow。 –

+0

@ThomasMatthews是的,我已經搜查過了。你看,我的問題不在於如何提出「一個答案」,因爲我的代碼給出了一個答案,而是關於找到所有的路線。我並沒有明白你的意思,我應該把這個循環。 –

回答

1

從我的意見之後,該代碼應工作:

#include <iostream> 
using namespace std; 

const int ROW_COUNT = 6; 
const int COL_COUNT = 6; 
const int POSSIBLE_MOVES = 8; 

int row_delta[POSSIBLE_MOVES] = {2, 1, -1, -2, -2, -1, 1, 2}; 
int col_delta[POSSIBLE_MOVES] = {-1, -2, -2, -1, 1, 2, 2, 1}; 

int board[ROW_COUNT][COL_COUNT]; 

void print_board() { 
    for (int i = 0; i < ROW_COUNT; i++) { 
     for (int j = 0; j < COL_COUNT; j++) { 
      if (board[i][j] < 10) 
       cout << ' '; 
      cout << board[i][j] << ' '; 
     } 
     cout << endl; 
    } 
    cin.get(); 
} 

find_tour(int move_no, int current_row, int current_col) { 
    // uncomment the following two lines for debugging: 
    //cout << move_no << endl; 
    //print_board(); 

    if (move_no == ROW_COUNT * COL_COUNT) 
    { 
     print_board(); 
     return; 
    } 

    for (int move = 0; move < POSSIBLE_MOVES; move++) { 
     int new_row = current_row + row_delta[move]; 
     int new_col = current_col + col_delta[move]; 

     if (new_row < 0 || new_row >= ROW_COUNT || new_col < 0 || new_col >= COL_COUNT) 
      continue; 

     if (board[new_row][new_col] != 0) 
      continue; 

     board[new_row][new_col] = move_no + 1; 
     find_tour(move_no + 1, new_row, new_col); 
     board[new_row][new_col] = 0; 
    } 
} 

void solve(int init_row, int init_col) { 
    for (int row = 0; row < ROW_COUNT; row++) 
     for (int col = 0; col < COL_COUNT; col++) 
      board[row][col] = 0; 

    board[init_row][init_col] = 1; 
    find_tour(1, init_row, init_col); 
} 

int main() { 
    solve(2, 3); 
}