"The beauty of programming" Chinese Chess Generals problem

Source: Internet
Author: User

Problem description

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.

My solution.
#include <stdio.h>int main (void) {unsigned char chpos = 0x11;for (; (Chpos & 0x0f) <= 9; Chpos + = 0x01) {for (; (Chpos >> 4) <= 9; Chpos + = 0x10) {if (((Chpos & 0x0f)-1)% 3 = = ((((Chpos >> 4) 1)% 3)) continue;printf ("B = (%c,%d) A = (% C,%d) \ n ", ' d ' + ((Chpos & 0x0f)-1)% 3, 1 + ((Chpos & 0x0f)-1)/3, ' d ' + ((Chpos >> 4)-1)% 3   , 8 + ((Chpos >> 4)-1)/3);} Chpos &= 0x0f;chpos |= 0x10;} return 0;}

As long as a byte, generals can only be moved at 9 points, so the first four bits represent the location of the handsome (1~9), the last four digits, then 3 is the column number, except 3 is the line number, as long as the column number is different can be guaranteed.

"The beauty of programming" 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.