Generate a Rubik Cube (Wen Ji 05-03)

zhaozj2021-02-16  63

I have accepted the school C #, and I want to look at the algorithm again.

Book bought a lot, what "21 days of learning C #", "C # primer" and "Microsoft .NET Framework Design (Revised)". Of course, the choice of these books is that I have read a lot of book reviews, I'm looking at it is a good choice, good book!

Although the book is good but also people, I use my spare time. Basic browsing, and more detailed knowledge points will be well mastered in the future.

Speaking of this, saying that you will not believe that I am still a small head in the unit, and I have a little old and old in the next day, I'm going up and down "qi", only after get off work, I will find a moment in the book. The purpose of reading is that this visible is a sadness.

I will make up your mind, no matter how busy in the unit, write a study week, not a diary, the diary is definitely there is no time to write.

It's easy to be difficult, I have read a "Programmer" programmer "programmer", "The second edition" of the programmer, "there is a lot of algorithm examples, they originally written with C, I used C # to write it again, and then be a small tool to show it, which is more intuitive.

The first let's take a look at the generation of the Magic Cube in P96.

The Rubik's Cube is an element of Nature 1, 2, ..., N2 N × N square array, each element value is not equal, each line, column, and main, the sum of N elements on each of the adjacences, etc. .

To the extensive march, you can generate with the Dole Rob algorithm, the process starts from 1, insert each of the numbers until N2. Selecting the insertion position principle is:

a. The first position is in the first line;

b. The new location should be in the top right of the most inserted position, but if the location of the upper right is exceeded, the new position will be selected in the bottom position of the selected column; if it exceeds the right boundary, the new boundary should be selected. The leftmost position;

c. If the nearest integer multiple of the insertion element is N, select the same column of the same column below.

The implementation of this algorithm needs to use the two-dimensional integer array to implement the square.

The core algorithm is as follows:

Private void Button1_Click (Object Sender, System.Eventargs E)

{// This part is a Dole Rob algorithm that generates the magic square stroke.

Int Count, Curi, Curj

INT I, J;

Int [,] magic = new int =

String a;

Curi = 0;

Curj = 1;

For (count = 1; count <= 9; count )

{

Magic [Curi, Curj] = count

IF ((count% 3) == 0) // The most recent integer multiple of the insertion element is the order of the order, select the position on the same column below to a new location.

{

Curi = 1;

CONTINUE;

}

Curi = CURI-1;

Curj = CURJ 1;

IF (Curi <0)

Curi = 3;

Else IF (CURJ == 3)

Curj- = 3;

}

TextBox1.AppendText ("/ n"); // Show the generated Rubik in TextBox1

For (i = 0; i <3; i )

{TextBox1.AppendText ("/ n");

For (j = 0; j <3; j )

{A = convert.toString (Magic [i, j]);

TextBox1.AppendText (a);

TextBox1.AppendText ("");

}

TextBox1.AppendText ("/ n");

}

Label1.text = "The order of the magic square stroke in this example is taken here 3, of course, the control step can be increased, and it is no longer given here.";

}

Figure 1 Click "Generate the Magic Cube" button before

Figure 2 After clicking "Generate the Magic Cube" button

When rewriting change algorithms, there are several questions to me, I have a deep impression:

a. Magic [Curi, Curj] is written like this, rather than writing MAGIC [CURI], [CURJ], the latter is the write of C.

b. a = convert.toString (Magic [i, j]) This sentence is critical, otherwise the result cannot be displayed in TextBox1, and the Convert class is in the System namespace, and some of its methods are useful. Why is it to convert because a must be a String type in TextBox1.AppendText (a).

c. C # language is strictly sized.

Ok, let's write here, don't tell what you like, even if you are tired.

Tomorrow is another ...

转载请注明原文地址:https://www.9cbs.com/read-22302.html

New Post(0)