3-32 推箱子(数组元素移动)

这里写图片描述
结构体和二维数组的应用

#include <iostream>
using namespace std;

struct enter
{
    int row;
    int col;
};
int main()
{
    int array[4][4]={{1,1,0,1},{1,1,2,1},{1,0,0,1},{3,0,1,1}};//数组元素
    enter enter_box;    //箱子的位置
    enter_box.row=1;    //箱子的行
    enter_box.col=2;    //箱子的列

    for(int i=enter_box.row;i<4;i++)//从上到下,从右到左搜索元素为0的位置
    {
        for(int j=enter_box.col;j>=0;j--)
        {
            if(array[i][j]==0)  //继续搜索
            {
                enter_box.row=i;
                enter_box.col=j;
                cout<<"("<<enter_box.row<<","<<enter_box.col<<")->";
            }
            else if(array[i][j]==1)
            {}
            else if(array[i][j]==3)
            {
                cout<<"到达目的地"<<endl;
            }
            else
            {}
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值