/*题目:
在一个线性七个格位置的图上有两种不同颜色的棋子A,B。排列如下图所示,中间的位置
为空。
A A A B B B
要求将A,B的现行位置交换,形成下图中的排列:
B B B A A A
移动棋子的要求:
()每个格中只准放一个棋子。
()任意一个棋子均可移动一格放入空格内。
()任何的旗子不得跳跃两个或两个以上的旗子。(无论颜色相同)
()一方的旗子均可跳过另一方的旗子进入空格。
()任何一个颜色旗子只能向前跳,不能像后跳。
编程完成相关的移动。并得出总共有都少种走法。
最好用C或C++实现
*/
/*
算法分析:
只有一个空格,有两种走法:向前移动一格,向前跳一格;
不同的走法使空格的位置发生变化,而空格的位置又决定可以有那种走法;
所以应该有一个根据空格位置决定走法的函数;
而走法函数会使空格的位置发生变化;
走法一:移动的规则:
只能向前,即对于A其坐标只能变大一;对于B其坐标只能变小一;
只能跳入空格,即坐标对应值为空;
移动成功则空格坐标变为此坐标;再继续选择走法;
移动失败则不变,换其他走法;
走法二:跳动的规则:
只能向前,即对于A其坐标只能变大二;对于B其坐标只能变小二;
只能跳入空格,即坐标对应值为空;
A的坐标+处必须为B,B的坐标-处必须为A;
移动成功则空格坐标变为此坐标;再继续选择走法;
移动失败则不变,换其他走法;
所以综上所述,本程序需要一个数组char S[]=(A,A,A,' ',B,B,B);
需要标记空格位置的变量,int SpaceIndex;
需要记录走法数目的变量,int GoNum;
需要走动的函数用于选择具体的走法去走,void go();
需要具体走法的函数两个,void move();和void jump();
需要测试可以进行那种走法的函数:int test();
*/
#include <iostream>
#include <string.h>
using namespace std;
int GoNum=0;
char O[7]={'B','B','B',' ','A','A','A'};
void go(int* a_SpaceIndex,char S[]);
void move(int* a_SpaceIndex,char S[]);
void jump(int* a_SpaceIndex,char S[]);
int test(int* a_SpaceIndex,char S[]);
void print(char S[])
{
for(int i=0;i<7;i++)
cout<<S[i]<<',';
cout<<endl;
}
int main(void)
{
char S[7]={'A','A','A',' ','B','B','B'};
int SpaceIndex=3;
print(S);
go(&SpaceIndex,S);
cout<<GoNum<<endl;
return 0;
}
void go(int* a_SpaceIndex,char S[])
{
char t_S[7];
int t_SpaceIndex;
int f=test(a_SpaceIndex,S);
if(f&1 || f&2)
{
cout<<"in move"<<endl;
print(S);
t_SpaceIndex=*a_SpaceIndex;
strcpy(t_S,S);
move(a_SpaceIndex,S);
strcpy(S,t_S);
(*a_SpaceIndex)=t_SpaceIndex;
print(S);
cout<<"out move"<<endl;
}
if(f&4 || f&8)
{
cout<<"in jump"<<endl;
print(S);
t_SpaceIndex=*a_SpaceIndex;
strcpy(t_S,S);
jump(a_SpaceIndex,S);
strcpy(S,t_S);
(*a_SpaceIndex)=t_SpaceIndex;
print(S);
cout<<"out jump"<<endl;
}
if(f==0)
{
print(S);
cout<<"nothing!"<<endl;
if(strcmp(O,S)==0)
{
cout<<"Find it!"<<endl;
print(S);
GoNum++;
}
}
}
int test(int* a_SpaceIndex,char S[])
{
bool flag1,flag2,flag3,flag4;
int f=0;
flag1=(S[(*a_SpaceIndex)-1]=='A');
flag2=(S[(*a_SpaceIndex)+1]=='B');
flag3=(S[(*a_SpaceIndex)-2]=='A' && S[(*a_SpaceIndex)-1]=='B');
flag4=(S[(*a_SpaceIndex)+2]=='B' && S[(*a_SpaceIndex)+1]=='A');
if((*a_SpaceIndex)<=0)
flag1=false;
if((*a_SpaceIndex)>=6)
flag2=false;
if((*a_SpaceIndex)<=1)
flag3=false;
if((*a_SpaceIndex)>=5)
flag4=false;
f=f|(flag1*1);
f=f|(flag2*1*2);
f=f|(flag3*1*2*2);
f=f|(flag4*1*2*2*2);
return f;
}
void move(int* a_SpaceIndex,char S[])
{
int f;
char t_S[7];
int t_SpaceIndex;
f=test(a_SpaceIndex,S);
if(f&1)
{
t_SpaceIndex=*a_SpaceIndex;
strcpy(t_S,S);
S[*a_SpaceIndex]='A';
(*a_SpaceIndex)--;
S[*a_SpaceIndex]=' ';
go(a_SpaceIndex,S);
strcpy(S,t_S);
(*a_SpaceIndex)=t_SpaceIndex;
}
if(f&2)
{
t_SpaceIndex=*a_SpaceIndex;
strcpy(t_S,S);
S[*a_SpaceIndex]='B';
(*a_SpaceIndex)++;
S[*a_SpaceIndex]=' ';
go(a_SpaceIndex,S);
strcpy(S,t_S);
(*a_SpaceIndex)=t_SpaceIndex;
}
if(!(f&1) && !(f&2))//should always be false;using for debug;
{
cout<<"not move!!"<<endl;
}
}
void jump(int* a_SpaceIndex,char S[])
{
int f;
char t_S[7];
int t_SpaceIndex;
f=test(a_SpaceIndex,S);
if(f&4)
{
t_SpaceIndex=*a_SpaceIndex;
strcpy(t_S,S);
S[*a_SpaceIndex]='A';
(*a_SpaceIndex)-=2;
S[*a_SpaceIndex]=' ';
go(a_SpaceIndex,S);
strcpy(S,t_S);
(*a_SpaceIndex)=t_SpaceIndex;
}
if(f&8)
{
t_SpaceIndex=*a_SpaceIndex;
strcpy(t_S,S);
S[*a_SpaceIndex]='B';
(*a_SpaceIndex)+=2;
S[*a_SpaceIndex]=' ';
go(a_SpaceIndex,S);
strcpy(S,t_S);
(*a_SpaceIndex)=t_SpaceIndex;
}
if(!(f&4) && !(f&8))//should always be false;using for debug;
{
cout<<"not jump!"<<endl;
}
}