#include <fstream>
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;
}
int 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<<"對不起,找不到答案!"<<endl;
cout<<"對不起,找不到答案!"<<endl;
flag=0;
break;
}
}
}
Init_diamond();
answer<<"圖解答案如下:"<<endl;
for(int i=0; i<7; i++)
{
for(int j=0; j<7; j++)
{
answer<<diamond[i][j]<<' ';
}
answer<<endl;
}
answer<<endl<<endl;
// 輸出解
cout<<"下面是程序執行過程中棋子跳動的數據變化過程!"<<"\n";
for(int n=0; n<top; n++)
{
cout<< "step "<<n+1<<":"<<"\n" "Move diamond ("<<mystack[n].sy+1<<","<<mystack[n].sx+1<<") ---> ("<<mystack[n].ty+1<<","<<mystack[n].tx+1<<")"<<endl;
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] = '*';
cout<< "Left diamonds : "<<top-n<<endl;
for(int k=0; k<7; k++)
{
for(int j=0; j<7; j++)
{
answer<<diamond[k][j]<<' ';
}
answer<<endl;
}
answer<<endl<<endl;
}
if(flag)
{
cout<<"圖解答案已經打印到\"answer.txt\" 文件中!請查閱!"<<endl;
}
// system("pause");
return 0;
}