The problem of Chinese chess in the beauty of programming

Source: Internet
Author: User

I. Prototype of the topic

Chinese chess friends know that both sides of the "will" and "handsome" far apart, and they can not face. In the game of chess, many masters can use this rule to get out of the exquisite killing. Suppose the board has only "will" and "handsome" two sons (as shown in Figure 1-3) for the following purposes, we agreed to use A for "will", B for "handsome", then A and b of the movement will be limited to their own 3x3 lattice, a, B can be horizontal or vertical movement of a grid, but can not move diagonally. When A and B are in a straight line, the chess game ends. In other words, A and B cannot be on a straight line, write a program to output all possible locations for a, B.

Second, mathematical abstraction

The general idea of this problem is to iterate over the possible locations of each A and B, then combine, judge, and output if the conditions are met. Based on this idea, we find that the position of a and B can be described by these 9 numbers. So how do you judge whether these two points are on a line? After careful study, we found that we can solve with the remainder, so the code of the program comes out:

for (int i = 1; I <= 9; i++)
            {for
                (int j = 1; J <= 9; j +)
                {
                    if (i% 3!= J% 3) {
                        Console. WriteLine ("A Possible combination is" + "(" + i + "," + j + ")");}}
            

Oh, the first "beauty of programming" article on such a happy start it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.