MyMSDN

MyMSDN记录开发新知道

中国象棋将帅问题

这是编程之美之中的一道题,编程之美中的解法固然好,但自己手写一遍还是印象深刻。话说,只有想不到,没有做不到。

C语言版(关键在思路,更好的表示,特别是whoIsOk1的表示,可能编程之美上的做法更工业化,这里为了简化说明问题,用了很多magic number):


/*
* chinese_chess_admiral_marshal.c
*
*  Created on: 2008-9-14
*      Author: Volnet
*      WebSite:
http://volnet.cnblogs.com/
*              
http://www.cppblog.com/mymsdn/
 
*/

#include <stdio.h>

#include <
stdlib.h>
/*
* admiral
*  1    2    3
*  4    5    6
*  7    8    9
*
* marshal
*  1    2    3
*  4    5    6
*  7    8    9
*
* who can't sit in the same column.
* e.g. if admiral in "1", the marshal can't in the "1"、"4"、"7".
*
*/
/*
* The five functions do the same thing.
*
*/
void whoIsOk1();
void whoIsOk2();
void whoIsOk3();
void whoIsOk4();
void whoIsOk5();
int main(void) {
    whoIsOk1();
    printf(
"-----------------------------------\n");
    whoIsOk2();
    printf(
"-----------------------------------\n");
    whoIsOk3();
    printf(
"-----------------------------------\n");
    whoIsOk4();
    printf(
"-----------------------------------\n");
    whoIsOk5();
   
//printf("-----------------------------------\n");
    return EXIT_SUCCESS;
}
void whoIsOk1() {
   
/*Normal one*/
    unsigned
int i, j, total;
    total
= 0;
   
for (i = 1; i <= 9; i++)
       
for (j = 1; j <= 9; j++)
           
if (i % 3 != j % 3) {
                printf(
"A=%d,B=%d\n", i, j);
                total
++;
            }
    printf(
"total = %d\n", total);
}
/*
* i:iiii0000
* j:0000jjjj
*
*/
/* g:a variable who can contains 8 bits.
*
*/
#define iGET(g) ((g&0xf0)>>4)
#define jGET(g) (g&0x0f)
#define iSET(g,i) (g=(g&0x0f)|((i)<<4))
#define jSET(g,j) (g=(g&0xf0)|(j))
void whoIsOk2() {
   
/*only one variable except “total”*/
    unsigned
char ij;
   
/* for testing
     ij = 0x53;
     iSET(ij,3);
     jSET(ij,5);
     printf("%x\n%d\n%d\n",ij,iGET(ij),jGET(ij));
*/
   
int total;
    total
= 0;
   
for (iSET(ij,1); iGET(ij) <= 9; iSET(ij,iGET(ij)+1))
       
for (jSET(ij,1); jGET(ij) <= 9; jSET(ij,jGET(ij)+1))
           
if (iGET(ij) % 3 != jGET(ij) % 3) {
                printf(
"A=%d,B=%d\n", iGET(ij), jGET(ij));
                total
++;
            }
    printf(
"total = %d\n", total);
}
void whoIsOk3() {
   
/* The principle of the function is :
     *     get the number 19 from one variable.
     *     get the number 19 from the same variable.
     *     we know the variable i/9 would mutex with i%9.
     *     e.g.
     *         int i = 20;
     *         i/7 == 2;
     *         i%7 == 6;
     *
     *         int j = 14;
     *         j/7 == 2;
     *         j%7 == 0;
     *
     *         i - j == 6;
     *         if(k/7 from 14 to 20)
     *             k%7 would from 0 to 6 (total = 7);
     *         so we can get the double circle from one variable.
     *
*/
    unsigned
int i = 81, total;
    total
= 0;
   
do {
       
if (i / 9 % 3 != i % 9 % 3) {
            printf(
"A=%d,B=%d\n", i / 9 + 1, i % 9 + 1);
            total
++;
        }
    }
while (i--);
    printf(
"total = %d\n", total);
}
/*whoIsOk3 is nearly equals to whoIsOk4*/
void whoIsOk4() {
    unsigned
int i = 81, total;
    total
= 0;
   
while (i--) {
       
if (i / 9 % 3 != i % 9 % 3) {
            printf(
"A=%d,B=%d\n", i / 9 + 1, i % 9 + 1);
            total
++;
        }
    }
    printf(
"total = %d\n", total);
}
typedef
struct {
    unsigned x :
4;
    unsigned y :
4;
} complex_counter;
void whoIsOk5() {
   
/*use an struct scheme to mix two variables in one*/
    complex_counter i;
    i.x
= i.y = 0;
    unsigned
int total;
    total
= 0;
   
for (i.x = 1; i.x <= 9; i.x++)
       
for (i.y = 1; i.y <= 9; i.y++)
           
if (i.x % 3 != i.y % 3) {
                printf(
"A=%d,B=%d\n", i.x, i.y);
                total
++;
            }
    printf(
"total = %d\n", total);
}

值得注意的一点是形如#define jSET(g,j) (g=(g&0xf0)|(j))这样的语句

为了避免二义性,也为了避免被宏替换后的操作符顺序依赖,尽量对使用的变量单独用()括起来,因为它不是一个字母在战斗……-_-"

执行结果(片段):

A=1,B=2
A=1,B=3
A=1,B=5
A=1,B=6
A=1,B=8
A=1,B=9
A=2,B=1
A=2,B=3
A=2,B=4
A=2,B=6
A=2,B=7
A=2,B=9
A=3,B=1
A=3,B=2
A=3,B=4
A=3,B=5
A=3,B=7
A=3,B=8
A=4,B=2
A=4,B=3
A=4,B=5
A=4,B=6
A=4,B=8
A=4,B=9
A=5,B=1
A=5,B=3
A=5,B=4
A=5,B=6
A=5,B=7
A=5,B=9
A=6,B=1
A=6,B=2
A=6,B=4
A=6,B=5
A=6,B=7
A=6,B=8
A=7,B=2
A=7,B=3
A=7,B=5
A=7,B=6
A=7,B=8
A=7,B=9
A=8,B=1
A=8,B=3
A=8,B=4
A=8,B=6
A=8,B=7
A=8,B=9
A=9,B=1
A=9,B=2
A=9,B=4
A=9,B=5
A=9,B=7
A=9,B=8
total = 54

posted on 2008-09-14 11:57 volnet 阅读(874) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


特殊功能