html5独立钻石棋,独立钻石跳棋问题的C++实现

/**********************************说明***********************************/

/*

问题描述:

独立钻石跳棋问题。在下图中,33个方格顶点摆放着32枚棋子,仅中央的顶点空着未摆放棋子。

下棋的规则是任一棋子可以沿水平或成垂直方向跳过与其相邻的棋子,进入空着的顶点并吃掉被跳过的棋子。

试设计一个算法找出一种下棋方法,使得最终棋盘上只剩下一个棋子在棋盘中央。

0------------------------->x

| 01 2 3456

|       * * *         0                                                   3

|       * * *         1                                                    |

| * * * * * * *    2                                                    |

| * * * .  * * *    3      方向确定规定为: 20

| * * * * * * *    4                                                    |

|      * * *          5                                                    |

|      * * *         6                                                    1

y

*/

/*************************************************************************/

#include

#include

using namespace std;

struct step //记录移动棋子的信息

{

int sx, sy; // 记录移动棋子前棋子的位置

int tx, ty; // 记录移动棋子后棋子的位置

int dir; // dir值代表移动棋子的方向

};

struct step mystack[100], last_step;

char diamond[7][7];

int Left_diamond = 32;

int x, y, nx, ny, ndir, top; // ndir值代表方向, 0代表向右, 1代表向下, 2代表向左, 3代表向上

int flag=1; // 是否成功找到解的标志

/*****************************初始化棋盘*****************************/

void Init_diamond()

{

for(int i=0; i<7; i++)

{

for(int j=0; j<7; j++)

{

if((i<2 || i>4) && (j<2 || j>4));

else

{

diamond[i][j] = '*';

}

}

}

diamond[3][3] = '.';

}

/*********************************************************************/

/*******************************移动棋子******************************/

int Move_diamond(int y, int x, int dir)

{

if(diamond[y][x] != '*')

{

return 0;

}

struct step temp;

switch(dir)

{

case 0:

if(x+2>6 || diamond[y][x+1]!='*' || diamond[y][x+2]!='.')

{

return 0;

}

diamond[y][x] = diamond[y][x+1] = '.';

diamond[y][x+2] = '*';

temp.sx = x;

temp.sy = y;

temp.tx = x+2;

temp.ty = y;

temp.dir = dir;

mystack[top++] = temp;

return 1;

break;

case 1:

if(y+2>6 || diamond[y+1][x]!='*' || diamond[y+2][x]!='.')

{

return 0;

}

diamond[y][x] = diamond[y+1][x] = '.';

diamond[y+2][x] = '*';

temp.sx = x;

temp.sy = y;

temp.tx = x;

temp.ty = y+2;

temp.dir = dir;

mystack[top++] = temp;

return 1;

break;

case 2:

if(x-2<0 || diamond[y][x-1]!='*' || diamond[y][x-2]!='.')

{

return 0;

}

diamond[y][x] = diamond[y][x-1] = '.';

diamond[y][x-2] = '*';

temp.sx = x;

temp.sy = y;

temp.tx = x-2;

temp.ty = y;

temp.dir = dir;

mystack[top++] = temp;

return 1;

break;

case 3:

if(y-2<0 || diamond[y-1][x]!='*' || diamond[y-2][x]!='.')

{

return 0;

}

diamond[y][x] = diamond[y-1][x] = '.';

diamond[y-2][x] = '*';

temp.sx = x;

temp.sy = y;

temp.tx = x;

temp.ty = y-2;

temp.dir = dir;

mystack[top++] = temp;

return 1;

break;

default:

break;

}

return 0;

}

/*********************************************************************/

/*******************************主函数********************************/

void main()

{

// 输出一个解到文本文件answer.txt

ofstream answer("answer.txt");

Init_diamond();

top = nx = ny = ndir = 0;

// 回溯遍历,直到找到一个解

while(1)

{

if(Left_diamond == 1 && diamond[3][3] == '*')

{

break;

}

for(y=ny; y<7; y++,nx=0)

{

for(x=nx; x<7; x++,ndir=0)

{

for(int dir=ndir; dir<4; dir++)

{

if(Move_diamond(y, x, dir))

{

Left_diamond--;

nx = ny = ndir = 0;

goto nextstep;

}

}

}

}

nextstep:

if(y == 7)

{

top--;

// 回到上一步, 并改变方向

if(top >= 0)

{

last_step = mystack[top];

diamond[(last_step.sy + last_step.ty)/2][(last_step.sx + last_step.tx)/2] = '*';

diamond[last_step.sy][last_step.sx] = '*';

diamond[last_step.ty][last_step.tx] = '.';

nx = last_step.sx;

ny = last_step.sy;

ndir = last_step.dir + 1;

Left_diamond++;

}

else

{

answer<

cout<

flag=0;

break;

}

}

}

Init_diamond();

answer<

for(int i=0; i<7; i++)

{

for(int j=0; j<7; j++)

{

answer<

}

answer<

}

answer<

// 输出解

for(int n=0; n

{

answer<

< ("<

diamond[mystack[n].sy][mystack[n].sx] = '.';

diamond[(mystack[n].sy+mystack[n].ty)/2][(mystack[n].sx+mystack[n].tx)/2] = '.';

diamond[mystack[n].ty][mystack[n].tx] = '*';

answer<

for(int k=0; k<7; k++)

{

for(int j=0; j<7; j++)

{

answer<

}

answer<

}

answer<

}

if(flag)

{

cout<

}

}

/*********************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值