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 write the sample code for the implementation of serpentine matrix with Cpicket +

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to write the sample code about the realization of the serpentine matrix with Cpicket +. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Chicken Chicken would like to record some algorithm learning experience in the blog, and will continue to update the answers to the questions in Chammer +, so as to make it easy to sort out the train of thought and review in the future. If you can still get to know the little friends who knock the code together, it's even better, because it's so weak, it's bound to be full of mistakes. Welcome criticism and correction and look forward to growing together!

Topic description

A serpentine square matrix with a positive integer n not greater than 9 and an output n × n is given.

Start with 1 in the upper left corner and fill in the numbers in turn clockwise, as shown in the example. Note that each number takes up 3 characters, preceded by a space.

Input exampl

Input

four

Output

1 2 3 4

12 13 14 5

11 16 15 6

10 9 8 7

Problem solving part

Algorithm involved: simulation.

Have you ever heard of the word "modeling"? The so-called "modeling" is to abstract things and build corresponding mathematical models according to actual problems. "abstraction" does not mean obscure; on the contrary, it provides a lot of convenience. It is difficult for the computer to solve the practical problem directly, but if the actual problem is modeled as a mathematical problem, it will be greatly convenient for the computer to "understand" and "solve".

Train of thought

1. First of all, we can abstract the problem into a mathematical problem, which can be understood as filling in natural numbers in a box according to a certain rule, as shown in the figure below.

(the ugly painting is lightly sprayed by everyone)

It can be seen that the trend of "Little Snake" is right, bottom, left, upper and repeated cycles.

two。 After modeling, we can represent the matrix as a two-dimensional array, and each number filled in is equivalent to a change in x or y.

Note that the establishment of this coordinate system is based on the characteristics of the two-dimensional array, where x represents rows and y represents columns.

Int map [15] [15]; / / although the maximum data required for the topic is 9: 9, the array space is usually enlarged in order to avoid memory explosion. For (iSuppli 1 in | | txn | | txn | | tx

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