UVa 227

题目链接:https://odzkskevi.qnssl.com/c142bcbb43aac35f2fe53a8b2d7dd133?v=1510619218


题意就是给你一个5*5的迷宫,有一个空格块,一共有4种指
令:A, B, L, R,分别表示把空格上、下、左、右的相邻字母移到空格中。输入初始网格和指
令序列(以数字0结束),输出指令执行完毕后的网格

记住,网格是5*5

指令是遇到数字0截止


My ugly code

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <map>

using namespace std;

string s[6];

int posx,posy;
int cas=1;
void sear(){

    posx=0;
    posy=0;

    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            if(s[i][j]==' '){
                posx=i;
                posy=j;
                return ;
            }
        }
    }

    return ;
}

int main(){

    /*freopen("input.txt","r",stdin);*/
    while(getline(cin,s[0]) && s[0][0]!='Z'){
        getline(cin,s[1]);
        getline(cin,s[2]);
        getline(cin,s[3]);
        getline(cin,s[4]);

        /*cout << s[0] << endl;
        cout << s[1] << endl;
        cout << s[2] << endl;
        cout << s[3] << endl;
        cout << s[4] << endl;*/

        sear();
        string ope;
        string ppp;
        while(getline(cin,ppp) && ppp[ppp.size() - 1] != '0'){
            ope+=ppp;
        }
        ope+=ppp;
        int len=ope.size();

        int flag=1;
        for(int i=0;i<len;i++){
            if(ope[i] == 'A'){
                if(posx-1 < 0){
                    flag=0;
                    break ;
                }
                char tmp=s[posx][posy];
                s[posx][posy]=s[posx-1][posy];
                s[posx-1][posy]=tmp;
                posx--;
            }
            else if(ope[i] == 'B'){
                if(posx+1 >= 5){
                    flag=0;
                    break ;
                }
                char tmp=s[posx][posy];
                s[posx][posy]=s[posx+1][posy];
                s[posx+1][posy]=tmp;
                posx++;
            }
            else if(ope[i] == 'R'){
                if(posy+1 >= 5){
                    flag=0;
                    break ;
                }
                char tmp=s[posx][posy];
                s[posx][posy]=s[posx][posy+1];
                s[posx][posy+1]=tmp;
                posy++;
            }
            else if(ope[i] == 'L'){
                if(posy-1 < 0){
                    flag=0;
                    break ;
                }
                char tmp=s[posx][posy];
                s[posx][posy]=s[posx][posy-1];
                s[posx][posy-1]=tmp;
                posy--;
            }
        }
        if(cas != 1){
            printf("\n");
        }
        printf("Puzzle #%d:\n",cas++);
        if(flag)
            for(int i=0;i<5;i++){
                printf("%c",s[i][0]);
                for(int j=1 ; j<5 ; j++){
                    printf(" %c",s[i][j]);
                }
                printf("\n");
            }


        else
            printf("This puzzle has no final configuration.\n");
    }

    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值