# 2011-01-01  编程之美 1.2 中国相帅问题的一个简洁解法

题目意思大概是，象棋棋盘上只有一相一帅，只能在9宫格内移动，且不能对面。要求给出相帅所有的位置的可能性，只能用一个变量。

只用一个变量，第一感是用位操作。给九宫格编个号：

```
1 2 3
4 5 6
7 8 9
```

遍历所有的位置，如果 位置编号 mod 3相同，说明在同一列中。按此思路，书中位操作的解法略显繁杂。这里一个简洁的的解法是：

```
void main()
{
    short unsigned int x;
    for (x = 0; (x & 0xF0) < 0x90; x += 0x10 ) {
        for ( x &= 0xF0; (x & 0x0F) < 9; x++)
            if ((x >> 4) % 3 != (x & 0x0F) % 3)
                printf("A = %d, B = %d\n", (x >> 4) + 1, (x & 0x0F) + 1);
    }
}
```

如果不用位操作的话，另外一个解法比较赞：

```
BYTE i = 81;
while (i--)
{
    if (i / 9 % 3 != i % 9 % 3)
        printf("A = %d, B = %d\n", i / 9 + 1, i % 9 + 1);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.log4think.com/bop-1-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
