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

来源:互联网 发布:sql server清空数据库 编辑:程序博客网 时间:2024/05/29 19:32

这道题,主要学习了
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 100005char 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;}
0 0
原创粉丝点击