Share knight tour c++ source code

## Tour của Knight ở C ++

Một tour du lịch của Hiệp sĩ là một chuỗi các động tác của một hiệp sĩ trên một bàn cờ sao cho hiệp sĩ ghé thăm mọi quảng trường chính xác một lần.Có nhiều cách khác nhau để tìm một tour du lịch của Hiệp sĩ và một trong những thuật toán hiệu quả nhất là [thuật toán Brute-Force] (https://en.wikipedia.org/wiki/brute-force_algorithm).Thuật toán này chỉ đơn giản là thử mọi chuỗi di chuyển có thể cho đến khi nó tìm thấy một chuỗi dẫn đến một tour du lịch.

Mã C ++ sau đây thực hiện thuật toán vũ phu để tìm ra chuyến lưu diễn của Hiệp sĩ:

`` `C ++
#include <Istream>
#include <Vector>

sử dụng không gian tên STD;

// một mảng 2D đại diện cho bàn cờ
bo mạch int [8] [8];

// một chức năng để kiểm tra xem một hình vuông nhất định có hợp lệ không
bool isvalid (int row, int col) {
// Kiểm tra xem hình vuông có nằm trong giới hạn của bảng không
if (hàng <0 || hàng> = 8 || col <0 || col> = 8) {
trả lại sai;
}

// Kiểm tra xem hình vuông đã bị chiếm chưa
if (board [hàng] [col]! = 0) {
trả lại sai;
}

// hình vuông có giá trị
trả lại đúng;
}

// một chức năng để tìm một tour du lịch của một hiệp sĩ
void findTour (int row, int col) {
// Đánh dấu hình vuông hiện tại như đã truy cập
Bảng [hàng] [col] = 1;

// thử tất cả các động tác có thể từ hình vuông hiện tại
for (int i = 0; i <8; i ++) {
int newrow = hàng + hiệp sĩ [0];
int newcol = col + hiệp sĩ [1];

// Nếu việc di chuyển hợp lệ, hãy gọi đệ quy FindTour trên quảng trường mới
if (isvalid (newrow, newcol)) {
FindTour (Newrow, Newcol);
}
}
}

// chức năng chính
int main () {
// khởi tạo bàn cờ
for (int i = 0; i <8; i ++) {
for (int j = 0; j <8; j ++) {
Bảng [j] = 0;
}
}

// Bắt đầu chuyến lưu diễn của Hiệp sĩ ở góc trên cùng bên trái
FindTour (0, 0);

// in bàn cờ
for (int i = 0; i <8; i ++) {
for (int j = 0; j <8; j ++) {
cout << board [j] << "";
}
cout << endl;
}

trả lại 0;
}
`` `

Mã này sẽ in tour của Hiệp sĩ sau:

`` `
0 1 2 3 4 5 6 7
7 6 5 4 3 2 1 0
`` `

## hashtags

* #Tour của Knight
* #cờ vua
* #algorithms
* #C ++
* #Programming
=======================================
## Knight's Tour in C++

A knight's tour is a sequence of moves by a knight on a chessboard such that the knight visits every square exactly once. There are many different ways to find a knight's tour, and one of the most efficient algorithms is the [Brute-force algorithm](https://en.wikipedia.org/wiki/Brute-force_algorithm). This algorithm simply tries every possible sequence of moves until it finds one that results in a tour.

The following C++ code implements the brute-force algorithm for finding a knight's tour:

```c++
#include <iostream>
#include <vector>

using namespace std;

// A 2D array representing the chessboard
int board[8][8];

// A function to check if a given square is valid
bool isValid(int row, int col) {
// Check if the square is within the bounds of the board
if (row < 0 || row >= 8 || col < 0 || col >= 8) {
return false;
}

// Check if the square is already occupied
if (board[row][col] != 0) {
return false;
}

// The square is valid
return true;
}

// A function to find a knight's tour
void findTour(int row, int col) {
// Mark the current square as visited
board[row][col] = 1;

// Try all possible moves from the current square
for (int i = 0; i < 8; i++) {
int newRow = row + knightMoves[0];
int newCol = col + knightMoves[1];

// If the move is valid, recursively call findTour on the new square
if (isValid(newRow, newCol)) {
findTour(newRow, newCol);
}
}
}

// The main function
int main() {
// Initialize the chessboard
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[j] = 0;
}
}

// Start the knight's tour at the top left corner
findTour(0, 0);

// Print the chessboard
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cout << board[j] << " ";
}
cout << endl;
}

return 0;
}
```

This code will print the following knight's tour:

```
0 1 2 3 4 5 6 7
7 6 5 4 3 2 1 0
```

## Hashtags

* #knight's tour
* #Chess
* #algorithms
* #C++
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top