227 - Puzzle

来源:互联网 发布:好看的喜剧电影知乎 编辑:程序博客网 时间:2024/06/07 10:05

switch的break;又忘了。。。。。

#include<stdio.h>#include<string.h>int main(){    char a[6][6];    char m,t;    int flag,Base=0;    int i,j;    int k1,k2;    while(gets(a[0])){++Base;        if(!strcmp(a[0],"Z"))            return 0;        gets(a[1]); gets(a[2]); gets(a[3]); gets(a[4]);flag=1;        for(i=0;i<=4;++i)            for(j=0;j<=4;++j){                if(a[i][j]==' '){                    k1=i;k2=j;break;                }            }        while(scanf("%c",&m)&&m!='0'){            switch(m){                case 'A':                    if(k1){                        t=a[k1][k2];a[k1][k2]=a[k1-1][k2];a[k1-1][k2]=t;                        --k1;                    }                    else                        flag=0;                        break;                case 'B':                    if(k1-4){                        t=a[k1][k2];a[k1][k2]=a[k1+1][k2];a[k1+1][k2]=t;                        ++k1;                    }                    else                        flag=0;                        break;                case 'L':                    if(k2){                        t=a[k1][k2];a[k1][k2]=a[k1][k2-1];a[k1][k2-1]=t;                        --k2;                    }                    else                        flag=0;                        break;                case 'R':                    if(k2-4){                        t=a[k1][k2];a[k1][k2]=a[k1][k2+1];a[k1][k2+1]=t;                        ++k2;                    }                    else                        flag=0;                        break;            }        }        if(Base-1)            printf("\n");         printf("Puzzle #%d:\n",Base);         if(flag){             for(i=0;i<5;++i){                    printf("%c",a[i][0]);                for(j=1;j<5;++j){                    printf(" %c",a[i][j]);                }                printf("\n");            }         }         else            printf("This puzzle has no final configuration.\n");        getchar();    }    return 0;}

0 0
原创粉丝点击