编程之美学习笔记:中国象棋将帅问题

来源:互联网 发布:麦哲伦星系 知乎 编辑:程序博客网 时间:2024/05/29 13:48
// ChessTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "windows.h"void Test1(){BYTE i = 81;while(i--){if (i / 9 % 3 == i % 9 % 3){continue;}printf("A = %d, B = %d\n", i / 9 + 1, i % 9 + 1);}}struct  {unsigned char a:4;unsigned char b:4;} i;void Test2(){for (i.a = 1; i.a <= 9; i.a++){for (i.b = 1; i.b <= 9; i.b++){if (i.a % 3 == i.b % 3){printf("A = %d, B = %d\n", i.a, i.b);}}}}#define HALF_BITS_LENGTH 4#define FULLMASK 255#define LMASK ( FULLMASK << HALF_BITS_LENGTH )#define RMASK ( FULLMASK >> HALF_BITS_LENGTH )#define REST(b, n) ( b = ( (LMASK & b) ^ n) )#define LSET(b, n) ( b = ( (RMASK & b) ^ (n << HALF_BITS_LENGTH) ) )#define RGET(b) (RMASK & b)#define LGET(b) ((LMASK & b) >> HALF_BITS_LENGTH)#define  GRIDW 3  //将帅移动范围的行宽度void Test3(){unsigned char b;for (LSET(b, 1); LGET(b) <= GRIDW*GRIDW; LSET(b, (LGET(b) + 1))){for (REST(b, 1); RGET(b) <= GRIDW*GRIDW; REST(b, (RGET(b) + 1))){if (LGET(b) % GRIDW != RGET(b) % GRIDW){printf("A = %d, B = %d\n", LGET(b), RGET(b));}}}}int _tmain(int argc, _TCHAR* argv[]){Test1();//Test2();//Test3();return 0;}


待完善。。。

0 0