The problem of Chinese chess generals

Source: Internet
Author: User

The problem of Chinese chess generals

Flyfish 2015-8-11

Questions quoted from "The Beauty of programming" Chinese Chess Generals
Generals each with only one step, forward, backward, traverse can be, but not out of the "nine", is limited to the 3x3 lattice movement. Will and handsome not in the same straight line directly opposite.
Please write a program that outputs generals all legal locations, requiring only one variable in the code.
The Convention uses a to mean "will," B means "handsome."

One solution is about bit manipulation skipping

The original text provides a solution

struct {    unsignedchar a:4;    unsignedchar b:4;} i;    for19;i.a++)        for1;i.b <=9;i.b++)            if33)                printf("A=%d,B=%d\n",i.a,i.b);

The idea is to split a variable into two parts

The original text provides solution two

81;while (i--){    if9393)        continue;    printf ("A = %d, B = %d\n"9191);}

Problem conversion
There is a two-digit m, the number on the 10 bit is a, the digits are B
The number of each bit can only be from 1 to 9, then there is a common 9*9=81 species composition

a[1,9]={a|1?a?9}
b[1,9]={b|1?b?9}

Two-digit number series
11, 12, 13, 14, 15, 16, 17, 18, 19
21, 22, 23, 24, 25, 26, 27, 28, 29
...

When the current label starts at 0, the number is reduced by 1, and it becomes a 9-decimal number, full 9 1.
10, 11, 12, 13, 14, 15, 16, 17, 18
20, 21, 22, 23, 24, 25, 26, 27, 28
...

10 binary 81, 9 binary is 100
9 Binary representation
10-digit number = M/9
Single digit = m% 9

Generals only 3 columns can be moved, generals cannot be in the same column, that is, 9* (9-3) = 54 Ways of Composition

A%3! = b%3

Since the subscript starts at 0, the individual numbers have been reduced by 1, and the output needs to be 1
M/9 + 1
M% 9 + 1

about Loops
12-digit Maximum 99 so the loop can be decremented from 99 onwards
2 The number on each bit can only be from 1 to 9 in 81 ways, you can decrement from 81
3 valid positions are 54, so you can decrease from 54 onwards
4 If the position of a and B match the conditions, then the position after the swap is also eligible, you can start the loop from 27

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The problem of Chinese chess generals

Related Article

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.