问题:
在一个有七个格子的盒子里,左边放着3个白球,第四个是空的,右边三个是黑球
游戏规则:
1.一次只能移动一个棋子
2.棋子可以向空格中移动,也可以跳过一个对方的棋子进入空格。
3.白色棋子只能向右移动,黑色棋子只能向左移动,不能跳过两个子。
#include <stdio.h> #include <stdlib.h> int number; void print(int a[]); void change(int *n, int *m); /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int t[7] = {1,1,1,0,2,2,2}; int i, flag; print(t); //若没有完成棋子的交换,则继续进行循环 while(t[0]+t[1]+t[2] != 6 || t[4]+t[5]+t[6] != 3) { flag = 1;//flag为棋子移动一步的标记,flag=1表示棋子尚未移动,否则表示已经移动 for(i=0; flag&& i<5; i++) if(t[i]==1 && t[i+1]== 2 && t[i+2]==0) { change(&t[i], &t[i+2]); print(t); flag = 0; } for(i=0; flag && i<5; i++) if(t[i] == 0 && t[i+1]==1 && t[i+2]==2) { change(&t[i], &t[i+2]); print(t); flag=0; } for(i=0; flag&&i<6; i++) if(t[i]==1&&t[i+1]==0&&(i==0 || t[i-1]!= t[i+2])) { change(&t[i], &t[i+1]); print(t); flag=0; } for(i=0; flag&&i<6; i++) if(t[i]==0 && t[i+1]==2&&(i==5 || t[i-1] != t[i+2])) { change(&t[i], &t[i+1]); print(t); flag=0; } } return 0; } void print(int a[]) { int i; printf("No. %2d:................\n", number++ ); printf(" "); for(i=0; i<=6; i++) printf(" | %c",a[i]==1?'*': (a[i]==2?'@':' ')); printf(" \\n............\n\n"); } void change(int *n, int * m) { int temp; temp = *n; *n = *m; *m = temp; } /* No. 0:................ | * | * | * | | @ | @ | @ \n............ No. 1:................ | * | * | | * | @ | @ | @ \n............ No. 2:................ | * | * | @ | * | | @ | @ \n............ No. 3:................ | * | * | @ | * | @ | | @ \n............ No. 4:................ | * | * | @ | | @ | * | @ \n............ No. 5:................ | * | | @ | * | @ | * | @ \n............ No. 6:................ | | * | @ | * | @ | * | @ \n............ No. 7:................ | @ | * | | * | @ | * | @ \n............ No. 8:................ | @ | * | @ | * | | * | @ \n............ No. 9:................ | @ | * | @ | * | @ | * | \n............ No. 10:................ | @ | * | @ | * | @ | | * \n............ No. 11:................ | @ | * | @ | | @ | * | * \n............ No. 12:................ | @ | | @ | * | @ | * | * \n............ No. 13:................ | @ | @ | | * | @ | * | * \n............ No. 14:................ | @ | @ | @ | * | | * | * \n............ No. 15:................ | @ | @ | @ | | * | * | * \n............ 这道题目重在分析白棋和黑棋走的 规则。 */
原文地址:http://blog.csdn.net/orangeisnotapple/article/details/44859037