The 1th chapter game music--Chinese chess Generals problem

Source: Internet
Author: User

The problem of Chinese chess generals

Chinese chess inside the "will" and "handsome" each stay in their own nine lattice, one step can only move sideways or vertically, and both sides can not meet (can not be in the same vertical line). In the aftermath, some people will use this rule to get out of the wonderful kill trick. Assuming that one party's "will" is A, the other party's "handsome" is B, and now the two sides can appear in all legal positions, the required variable can only be saved with a single byte.

We use 1~9 numbers to represent the position of each lattice point in the order in which the rows are prioritized, as shown in. In this way, the current column number can be obtained by using the modulo redundancy operation, thus determining whether a and B are mutually exclusive.

"Solution one" implemented in C language
a more serious solution is to use a bitwise operation, set a char variable, the first four bytes to save a variable, the next four bytes to save a variable. #include<stdio.h>#defineHalf_bits_length 4//half of the storage unit length, i.e. 4 bits#defineFullmask 255//Memory unit All bit mask (mask), in binary representation, is 11111111#defineLmask (Fullmask << half_bits_length)//left four, 11110000#defineRmask (Fullmask >> half_bits_length)//four-digit right. 00001111#defineRSET (b,n) (b = (Lmask &b) | (n)))//set the right four bits of B to n#defineLSET (b,n) (b = (Rmask &b) | (n) << half_bits_length))//set the left four bits of B to n#defineRget (b) (rmask&b)//get the value of the right four digits of B#defineLGET (b) ((lmask&b) >> half_bits_length)//get the value of the left four digits of B#defineGRIDW 3//Nine Gongge boundary length intMain () {unsignedCharb;  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)); }        }    }     return 0;}

The same idea is implemented in Java as follows:
 Packagechapter1youxizhilechinesechess;/*** "Solution one" * Chinese chess Generals problem *@authorDELL **/ Public classChineseChess1 { Public Static Final intHalf_bits_length = 4;//half the length of the storage unit, this is 4bit.     Public Static Final intFullmask = 255;//The hidden code for all bits, which is 11111111     Public Static Final intLmask = (byte) (255 << half_bits_length) & 0x0ff;//unsigned left shift, 11110000     Public Static Final intRmask = (byte) (255 >>> half_bits_length);//unsigned Right shift, 00001111     Public Static intLSET (byteBintN) {//set the left half of B to n       return(Rmask & B) ^ ((byte) n <<half_bits_length); }         Public Static intRSET (byteBintN) {//The right half of B is n       return(Lmask & B) ^ (byte) n; }         Public Static intLGET (byteb) {//get the left half of B        return(Lmask & B) >>half_bits_length; }         Public Static intRget (byteb) {//get the right half of B        returnRmask &b; }     Public Static Final intGRIDW = 3;  Public Static voidMain (string[] args) {byteb = 0; b= (byte) LSET (b,8);        System.out.println (b);  for(B= (byte) LSET (b,1); LGET (b) <=gridw*gridw;b= (byte) LSET (B,lget (b) +1)){             for(B= (byte) RSET (b,1); Rget (b) <=gridw*gridw;b= (byte) RSET (B,rget (b) +1)){                if(LGET (b)%GRIDW! = Rget (b)%GRIDW) System.out.println ("A =" +lget (b) + ", B =" +Rget (b)); }        }    }}
"Solution Two"
 Packagechapter1youxizhilechinesechess;/*** Chinese Chess Generals problem * "solution two" *@authorDELL **/ Public classChineseChess2 { Public Static voidMain (string[] args) {//byte i = Bayi;//While (i!=0) {//if (i/9%3!=i%9%3)//System.out.printf ("a=%d, b=%d\n", i/9+1,i%9+1);//i--;//        }        bytei = 1;  while(i!=80){            if(i/9%3!=i%9%3) System.out.printf ("A=%d, b=%d\n", i/9+1,i%9+1); I++; }    }}

"Solution three"

Some say it is the most efficient:

//The problem of Chinese chess generals//"solution three"#include <stdio.h>struct{unsignedCharA:4; unsignedCharB:4;} i;intMain () { for(i.a=1; i.a<=9; i.a=i.a+1){         for(i.b=1; i.b<=9; i.b=i.b+1){            if(i.a%3!=i.b%3) printf ("A =%d, B =%d\n", I.A,I.B); }    }    return 0;}

The 1th chapter game music--Chinese chess Generals problem

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.