uva 227 c++

#include<iostream>
#include<string>
#define N 5
int main()
{

    int posx, posy;
    int puzzleNum = 1;
    std::string arr[N];
    std::string operate;
    std::string order;
    bool ok;
    while (1)
    {
        std::getline(std::cin, arr[0]);
        if (arr[0][0] == 'Z')break;
        if (puzzleNum != 1) std::cout << std::endl;
        for (int i = 1; i < N; i++)
        {
            std::getline(std::cin, arr[i]);
            
        }
        operate = "";
        while (1)
        {    
            std::getline(std::cin, order);
            operate += order;
            if (order[order.length() - 1] == '0') 
                break;
        }
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                if (arr[i][j] == ' ')
                {
                    posy = i + 1;
                    posx = j + 1;
                }
            }
        }
        ok = true;
        for (int i = 0; i < operate.length() && ok; i++)
        {
            if (operate[i] == 0) break;
            switch (operate[i])
            {
            case 'A':
                if (posy == 1) 
                {
                    ok = false;
                    break;
                }
                else
                {
                    arr[posy - 1][posx - 1] = arr[posy - 2][posx - 1];
                    arr[posy - 2][posx - 1] = ' ';
                    posy--;
                }
                break;
            case 'B':
                if (posy == 5)
                {
                    ok = false;
                    break;
                }
                else
                {
                    arr[posy - 1][posx - 1] = arr[posy][posx - 1];
                    arr[posy][posx - 1] = ' ';
                    posy++;
                }
                break;
            case 'R':
                if (posx == 5)
                {
                    ok = false;
                    break;
                }
                else
                {
                    arr[posy - 1][posx - 1] = arr[posy - 1][posx];
                    arr[posy - 1][posx] = ' ';
                    posx++;
                }
                break;
            case 'L':
                if (posx == 1)
                {
                    {
                        ok = false;
                        break;
                    }
                }
                else
                {
                    arr[posy - 1][posx - 1] = arr[posy - 1][posx - 2];
                    arr[posy - 1][posx - 2] = ' ';
                    posx--;
                }
                break;
            default:
                break;
            }
        }
        std::cout << "Puzzle #" << puzzleNum++ << ":" << std::endl;
        if (ok)
        {
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++) {
                    if (j)
                        std::cout << " ";
                    std::cout << arr[i][j];
                }
                std::cout << std::endl;
            }
        }
        else
        {
            std::cout << "This puzzle has no final configuration." << std::endl;
        }
        
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值