Beautiful programming Reading Notes 1.2-question about Chinese chess masters

Source: Internet
Author: User
Tags bitset

Http://blog.csdn.net/pipisorry/article/details/36380669

Problem:Friends who have played Chinese chess know that the "generals" and "Shuai" of both sides are far away from each other, and they cannot meet each other. In the chess mess, many experts can use this rule to get out of the subtle tactics. Suppose there are only two sub-accounts on the board: "Jiang" and "Shuai" (as shown in 1-3) (For the convenience of the following description, we agree to useAIndicates "yes ",B"Handsome "):

A,BThe second child is restricted to motion in the 3 × 3 lattice. For example, in the preceding table,ASquare {D10,F10,D8,F8} surrounded, andBSquare {D3,F3,D1,F1} surrounded. Each step,A,BA grid can be moved either horizontally or vertically, but not along the diagonal line. In addition,ANot availableBThat is to say,AAndBCannot be in the same vertical line (for exampleAInD10, soBYou cannotD1,D2 andD3 ).

Write a program and OutputA,BAll valid locations.Only one variable must be used in the code..


Analysis:

In the question, it is only necessary to determine whether or not he or she will face Shuai, but he or she will only be able to move in nine positions, so he only needs to formally Form 1 ~ 9.

The conventional solution in solution 1 is bit operation. Generally, a series of masks can be defined using macros or enumeration types.


Reference code:

/*************************************** *************************************//* 1.2 Chinese chess masters: Pipi 2014-7-1 *//****************************** **************************************** * *****/# include <stdio. h> typedef struct bitfield {unsigned char A: 4; unsigned char B: 4;} bit; void chesstest1 () {char column = 1; for (; column <= 3; column ++) {// select for (; column <= 9; column + = 3) {// A (and B) select printf ("A = % d, B = % d \ N ", column, column % 3 + 1); printf (" A = % d, B = % d \ n ", column, column % 3 + 1 + 3 ); printf ("A = % d, B = % d \ n", column, column % 3 + 1 + 6); printf ("A = % d, B = % d \ n ", column, (column + 1) % 3 + 1); printf (" A = % d, B = % d \ n ", column, (column + 1) % 3 + 1 + 3); printf ("A = % d, B = % d \ n", column, (column + 1) % 3 + 1 + 6);} column-= 9 ;}} void chesstest2 () {char I = 81; while (I --) {If (I/9% 3 = I % 9% 3) // I/9 (ID of a-1 [0 ~ 8]) % 3 (column of a); I % 9 (number of B-1 [8 ~ 0]) % 3 continue; // continueprintf ("A = % d, B = % d \ n", I/9 + 1, I % 9 + 1) ;}} void chesstest3 () {bit I; for (I. a = 1; I. A <= 9; I. A ++) for (I. B = 1; I. B <= 9; I. B ++) if (I. A % 3! = I. B % 3) // printf ("A = % d, B = % d \ n", I. a, I. b);} void main () {<span style = "font-family: Arial, Helvetica, sans-serif;"> chesstest3 (); </span> <span style = "font-family: Arial, Helvetica, sans-serif;" >}</span>


Note:

1. Check chesstest1. In fact, you only need printf to output positive solutions, as long as the AB column is different, a total of 3*3*2*3 = 54 Cases

2. It is difficult to think about chesstest2. You only need to note that the AB numbers cannot be the same (in the IF judgment statement) and the AB numbers are output only when the AB columns are different.

<Span style = "font-size: 12px;"> 3. chesstest3 uses the bitfield method. For more information about the bitfield, see http://blog.csdn.net/pipisorry/article/details/36220851. </span>

The concept of the bit domain "C programming language" is described on a single page, indicating that the application is mainly used to save space or directly access the bit, application scenarios such as the symbol table of the compiler and some hardware drivers. Network Development, Information encoding compression and decompression, bitmap, and other aspects may be used. The rest of the scenarios will mostly discuss how to save time and overhead, so as to make the program run more efficiently. 

In addition, there is also a bit-read-write method, which is a generic bitset provided by the STL library. It supports bitwise assignment operations and the overall bitwise operation, the most convenient thing is that the input/output stream is overloaded, so it is easy to check whether the corresponding bit is set correctly. Compared with bit domains, bitset does not support partial access of several BITs, which is the advantage of bit domains. However, a single byte is required for the question of being handsome, while the basic alignment of bitset is 4 bytes, so it may not meet the requirement of the question, however, it is a good choice for other bit operations.

The concept of the bit domain "C programming language" is described on a single page, indicating that the application is mainly used to save space or directly access the bit, application scenarios such as the symbol table of the compiler and some hardware drivers. I think network development, information encoding compression, decompression, bitmap, and other aspects may be used. Most of the other scenarios will discuss how to save time and overhead, so that the program can run more efficiently.
From: http://blog.csdn.net/pipisorry/article/details/36380669

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.