The first chapter game music Chinese Chess Generals problem __ Chinese chess generals problem

Source: Internet
Author: User

Requires only one variable to be implemented with a handsome position shown:


Solution One: Universal thinking

#define HALF_BITS_LENGTH 4

This value is half the length of the memory storage unit, which is 4bit in this question.

#define FULLMASK 255

This number represents the mask of a whole bit, in binary notation, it is 11111111.

#define LMASK (Fullmask << half_bits_length)

This macro represents the mask of left bits, in binary notation, which is 11110000.

#define RMASK (Fullmask >> half_bits_length)

This number represents the mask of right bits, in binary notation, which represents 00001111.

#define RSET (b, n) (b = (Lmask & B) ^ n))

This macro, set the right side of B to n

#define LSET (b, n) (b = (Rmask & B) ^ (n<< half_bits_length)))

This macro, set the left of B to n

#define Rget (b) (Rmask & B)

This macro gets the value on the right side of B.

#define Lget (b) ((Lmask & B) >>half_bits_length)

This macro gets the value of the left side of B.

#define GRIDW 3

This number represents the line width of the generals move range.

#include <stdio.h>

#define HALF_BITS_LENGTH 4

#define FULLMASK 255

#define LMASK (Fullmask << half_bits_length)

#define RMASK (Fullmask >> half_bits_length)

#define RSET (b, n) (b = (Lmask & B) ^ n))

#define LSET (b, n) (b = (Rmask & B) ^ (n<< half_bits_length)))

#define Rget (b) (Rmask & B)

#define Lget (b) ((Lmask & B) >>half_bits_length)

#define GRIDW 3

int main ()

{

Unsignedchar b;

For (LSET (b, 1); Lget (b) <= GRIDW * GRIDW; LSET (b, (Lget (b) + 1)))

For (RSET (b, 1); Rget (b) <= GRIDW * GRIDW; RSET (b, (Rget (b) + 1)))

if (Lget (b)% GRIDW!= rget (b)% GRIDW)

printf ("A =%d, B =%d\n", Lget (b), Rget (b));

Return0;

Solution Two: Ingenious thinking

struct {

Unsignedchar A:4;

Unsignedchar B:4;

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);

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.