UVa 227

题目:UVa--227

代码:

#include<stdio.h>
#include<string.h>
#define RE freopen("I:in.txt","r",stdin);
#define WE freopen("I:out.txt","w",stdout);
char a[5][5];

int main(){
    int cases = 1;
    int i , j , k , l;
//     RE;
//     WE;
    while(gets(a[0])){
        if(strcmp(a[0],"Z") == 0)
            break;
        gets(a[1]);
        gets(a[2]);
        gets(a[3]);
        gets(a[4]);
        char command[1000] = {'\0'} , subcommand[100];   //注意每一次都要将存放命令符号的数组清零
       do{
            gets(subcommand);
            l = strlen(subcommand);
            strcat(command,subcommand);
       }while(subcommand[l-1]!='0');                        //将不同行的命令符号放在同一数组中
        int line , row;
        for(i = 0;i < 5;i++)
            for(j = 0;j < 5;j++)
                if(a[i][j] == ' '){
                    line = i;
                    row = j;
                    break;
                }                                           //找出刚开始空格的位置
        int length = strlen(command);
        for(k = 0;k < length - 1;k++){
            if(command[k] == 'A'){
                if(line - 1 < 0)
                    break;                                  //越界
                else{
                a[line][row] = a[line - 1][row];
                a[line - 1][row] = ' ';
                line--;
                }
            }

            if(command[k] == 'B'){
                if(line + 1 > 4)
                    break;                                  //越界
                else{
                    a[line][row] = a[line + 1][row];
                    a[line + 1][row] = ' ';
                    line++;
                }

            }

            if(command[k] == 'L'){
                if(row - 1 < 0)
                    break;                                     //越界
                else{
                    a[line][row] = a[line][row - 1];
                    a[line][row - 1] = ' ';
                    row--;
                }
            }

            if(command[k] == 'R'){
                if(row + 1 > 4)
                    break;                                  //越界
                else{
                    a[line][row] = a[line][row + 1];
                    a[line][row + 1] = ' ';
                    row++;
                }
            }
        }

        if(cases!=1)
            printf("\n");                                   //相邻输出之间存在一空行
        printf("Puzzle #%d:\n",cases++);
        if(command[k] == '0'){
            for(i = 0;i < 5;i++)
                    printf("%c %c %c %c %c\n",a[i][0],a[i][1],a[i][2],a[i][3],a[i][4]);
        }
        else
            printf("This puzzle has no final configuration.\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值