独立钻石跳棋问题

//参考 :http://blog.sina.com.cn/s/blog_510ad0640100bo3d.html

#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;
int x, y, nx, ny, ndir, top; // ndir值代表方向, 0代表向右, 1代表向下, 2代表向左, 3代表向上
int endx, endy;  //终止方格坐标
int n;  //初始钻石个数

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))  //棋盘外
                continue;
            else
                diamond[i][j] = '!';  //空方格
        }
    }

    ifstream fin("独立钻石跳棋.txt");
    cout << "输入棋子数:";
    fin >> n;  cout << n << endl;
    Left_diamond = n;
    cout << "输入棋子位置:\n";
    int x, y;
    for(i=0; i<n; i++)
    {
        fin >> y >> x;  
        cout << x << " " << y << endl;
        diamond[x][y] = '*';  //钻石
    }
    cout << "输入终止方格坐标:";
    fin >> endx >> endy;
}


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()
{
    Init_diamond();

    top = nx = ny = ndir = 0;
    // 回溯遍历,直到找到一个解
    while(1)
    {
        if(Left_diamond == 1 && (diamond[endx][endy] == '*' || (endx==0 && endy==0))) //结束
        {
            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
            {
                cout<<"对不起,找不到答案!"<<endl;
                break;
            }
        }
    }

    // 输出解
    cout<<"下面是程序执行过程中棋子跳动的数据变化过程!"<<"\n";
    for(int n=0; n<top; n++)
        cout << "(" << mystack[n].sx <<","<< mystack[n].sy<<") ("<<mystack[n].tx<< "," <<mystack[n].ty<< ")" <<endl;

    return 0;
}

这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值