跳棋

来源:互联网 发布:没有目标知乎 编辑:程序博客网 时间:2024/04/28 05:35
#include <iostream>
#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;
}
0 0