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

An example Analysis of the Travel problem of C++ Knights

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of the travel problem of C++ knights, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Knight travel problem: make a knight traverse all the squares once and only once on the international chessboard, and output a path that meets the above requirements for any given vertex

Ideas for solving the problem:

This is a classic traversal problem (DFS). Because the title requires traversing all, then you must mark it, so you immediately think of the DFS depth-first algorithm. The specific ideas are as follows:

① knows chess and the moves of chess knights.

Chess and Chinese chess are more or less the same. After all, Chinese chess is an old ancestor. Chess pieces are placed in the grid, Chinese chess is placed on the spot, and chess has 64 squares. The knight of chess is the same as the horse of Chinese chess, and can walk in eight directions. The way to walk is to take the word "day", or the "L" shape of the English capital letter, that is, first go 1 square to the left (or right), then 2 squares up (or down), or 2 squares to the left (or right), and then 1 square up (or down). Unlike the Chinese chess game, chess horses can jump over other pieces on the road without the restriction of turning their feet.

Solving the problem requires that we can abstract the grid into a point, then the knight move of chess is a Japanese character.

② setting tag

Initialize the array, initializing each element to 0, and initializing a cal that records the number of times the knight traverses

Int cal = 0 / / initialize to 0int chress [8] [8] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

③ determines whether it is out of bounds and accessed.

Bool ifOut (int x, int y) / / determines whether it is out of bounds {if (x > = 0 & & x = 0 & & y = 0 & & y x > > y; / / enter coordinates if (x > 7 | | x 7 | | y < 0) cout

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

Development

Wechat

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

12
Report