紫书章三习题5——UVA 227 Puzzle

这道题,主要学习了
1.怎么将代码写的好看(简洁),之前 都是冗长冗长的,能不动脑子就不动脑子。switch,?:
2.freopen(“D:\input.txt”,”r”,stdin)的运用,提交的时候一定要加\
3.fgets(数组,maxn,stdin)
输入到数组中,长度为不超过maxn-1,然后在末尾加上\0,所以不会超空间。fgets是读取完整的一行,读到回车符截止,然后‘\n’也是这个数组中最后一个有效字符
4.这道题很容易卡输出PE,因为题目中说了,每两个结果之间要有有个空行,所以最后一个是没有空行的。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
#define maxn 100005
char s[10][10];
char op[100005];
int f[10]={-1,1,0,0};
int x1[10]={0,0,-1,1};
int main()
{
 //   freopen("D:\\input.txt","r",stdin);
    int temp=0;
    while(fgets(s[0],10,stdin)&&s[0][0]!='Z')
    {
        for(int i=1;i<5;i++)
            fgets(s[i],10,stdin);
        int x=0,y=0;
        for(int i=0;i<5;i++)
            for(int j=0;j<5;j++)
            {
                if(s[i][j]==' '||s[i][j]=='\n')
                {
                    x=i;y=j;
                    break;
                }
            }
        temp++;
        if(temp!=1)
            printf("\n");
        int stop=0,flag=0;
        char c;
        while((c=getchar())!=EOF&&c!='0')
        {
            int stop=0;
            switch(c)
            {
                case 'A':  {stop=0;break;}
                case 'B':  {stop=1;break;}
                case 'L' :  {stop=2;break;}
                case 'R':  {stop=3;break;}
                default: continue;
            }
            int x11=x,y1=y;
            x+=f[stop],y+=x1[stop];
            s[x11][y1]=s[x][y];
            if(x<0||y<0||x>=5||y>=5)
                flag=1;
        }
        printf("Puzzle #%d:\n",temp);
        if(flag==1)
            printf("This puzzle has no final configuration.\n");
        else
        {
            s[x][y]=' ';
            for(int i=0;i<5;i++)
            {
                for(int j=0;j<5;j++)
                    j==0? printf("%c",s[i][j]):printf(" %c",s[i][j]);
                printf("\n");
            }
        }
        getchar();//因为‘0’后面还有一个换行符
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值