Puzzle,UVa227

这道题因为输入输出折腾了许久....

我也是醉了...

还看了网上找到的AC样例....->这里是链接


这里需要注意的是如果你是直接复制粘贴测试样例到txt中,有可能原本边界处的空白字符会不见了...见了...了...

#include <stdio.h>
#include <iostream>
#include <string.h>

#define row_max 7
#define col_max 7

char puzzle[row_max][col_max];//定义puzzle的表达形式
char instru[1001];

using namespace std;

int main()
{
    memset(puzzle,0,sizeof(puzzle));
    int kase = 0;

    //while(scanf("%s",puzzle[0])&&(puzzle[0][0]!='Z'))

    while(gets(puzzle[0]))
    {
        if(puzzle[0][0] == 'Z') break;

        for(int i=1;i<5;i++)
            gets(puzzle[i]);

        int coor_x=0,coor_y=0;

        //打印一下输入结果
        /*
        printf("\n");
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<5;j++)
            printf("%c ",puzzle[i][j]);
            printf("\n");
        }
        */


        for(int i=0;i<5;i++)
        {
            for(int j=0;j<5;j++)
            {
                if(puzzle[i][j]==' ')
                {
                    //printf("find the space\n");
                    coor_x = i;
                    coor_y = j;
                    break;
                }
            }
        }




        int kount=0;

        while(~scanf("%c",&instru[kount]))
            if(instru[kount]!='0') kount++;
            else break;

        instru[kount] = '0';
       // instru[kount+1] ='\0';
        getchar();//为了回车键设计的,不会影响到下一次输入

        //打印指令
        //instru[kount+1]='\0';
        //printf("%s\n",instru);

        bool isillegal = false;

        int temp_x,temp_y;

        temp_x = coor_x;
        temp_y = coor_y;

        //未进入for之前的temp_x,temp_y是否合法:
        //printf("temp_x :%d temp_y:%d\nspace:%c\ncoor_x:%d,coor_y:%d\n",temp_x,temp_y,puzzle[temp_x][temp_y],coor_x,coor_y);

        for(int i=0;instru[i]!='0';i++)
        {
            switch(instru[i])
            {
                case 'A': temp_x=coor_x-1;temp_y=coor_y;break;
                case 'B': temp_x=coor_x+1;temp_y=coor_y;break;
                case 'L': temp_x=coor_x;temp_y=coor_y-1;break;
                case 'R': temp_x=coor_x;temp_y=coor_y+1;break;
            }

            if(temp_x<0||temp_x>4||temp_y<0||temp_y>4)
            {
                isillegal=true;break;
            }

            else
            {
                //printf("the normal behave\n");
                puzzle[coor_x][coor_y] = puzzle[temp_x][temp_y];
                puzzle[temp_x][temp_y] = ' ';
                coor_x = temp_x;
                coor_y = temp_y;
            }
        }
        if(kase++) printf("\n");

        printf("Puzzle #%d:\n",kase);

        if(isillegal)
        {
            printf("This puzzle has no final configuration.\n");
        }
        else
        {
            for(int i=0;i<5;i++)
            {
                printf("%c",puzzle[i][0]);
                for(int j=1;j<5;j++)
                {
                    printf(" %c",puzzle[i][j]);
                }
                printf("\n");
            }
        }

    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值