Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Cavaliers' Checkerboard algorithm in C++

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to realize the Knight's Chessboard algorithm in C++", so the editor summarizes the following contents to you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "C++ how to achieve knight chessboard algorithm" article.

1. Problem description

Cavaliers travel Knight tour attracted the attention of mathematicians and jigsaw puzzle fans at the beginning of the 18th century. When it was proposed can no longer be tested, the knight's move is chess, the knight can start from any position, and how to finish all the positions.

two。 Basic ideas

The knight's way of walking can basically be solved by handing back, but pure recursion is quite inefficient when the dimension is large. A clever solution was proposed by J.CWarnsdorff in 1823. To put it simply, the most difficult position will be finished first, and then the road will be widened. The next step that the knight wants is the least number of steps that can be taken for the next time no longer selected. Using this method, you can have a higher probability of finding a way to walk without using recursion (there is also a chance that you can't find it).

3. Code implementation # include int pos [8] [8] = {0}; int travel (int, int); int travel (int x, int y) {int I, j, k, l, m; int tmpX, tmpY; int count, min, tmp; / / eight directions (clockwise) int ktmoveX [8] = {1, 2, 2, 1,-1,-2,-2,-1} Int ktmoveY [8] = {- 2,-1, 1, 2, 1,-1,-2}; / / Test next coordinates int nextX [8] = {0}; int nextY [8] = {0}; / / record the number of exits in each direction int exists [8] = {0}; / start with 1 mark position I = x; j = y; pos [I] [j] = 1; / traverse the chessboard for (m = 2) M 7 | | tmpY > 7) {continue;} / / Walking record if (pos [tmpX] [tmpY] = = 0) {nextX [l] = tmpX; nextY [l] = tmpY; lager; / / Walkable direction plus 1}} count = l; / / there is no way to return if (count = = 0) {return 0; / / one direction walkable mark} else if (count = 1) {min = 0 / / find out the number of exits in the next location} else {for (l = 0; l)

< count; l++) { for (k = 0; k < 8; k++) { tmpX = nextX[l] + ktmoveX[k]; tmpY = nextY[l] + ktmoveY[k]; if (tmpX < 0 || tmpY < 0 || tmpX>

| 7 | | tmpY > 7) {continue;} if (pos [tmpX] [tmpY] = 0) {tmp [l] +;} / / find out the direction of the next location with the least exit min = 0; tmp = exists [0]; for (l = 0; l < count; lumped +) {if (exists [l] < tmp) {tmp = exists [l]; min = l I = nextX [min]; j = nextY [min]; pos [I] [j] = m;} return 1;} int main () {int I, j, startX, startY; while (1) {printf ("input starting point:"); scanf ("% d% d", & startX, & startY); if (travel (startX, startY)) {printf ("Travel completed!") ;} else {printf ("Travel failed!") ;} for (I = 0; I < 8; iTunes +) {for (j = 0; j < 8; pos +) {printf ("% 2d", pos [I] [j]);} printf (");} printf (");} return 0;}

The above is about the content of this article on "how to realize the Knight's Chessboard algorithm in C++". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report