UVA227--谜题

# include 
# include 

using namespace std;

int main(){

    char a[5][7],t;
    int n,m,i,j,k;



    int cases = 0;
    char modol[1001];
    while(gets(a[0])){

        if(a[0][0]=='Z') break; 

        for(i=1;i<5;i++){
            gets(a[i]);
        }


        //查找空格的位置
        int x = 0,y = 0; 
        for(i=0;i<5;i++)
            for(j=0;j<5;j++)
                if(a[i][j]==' '){
                 x=i,y=j;
                 break;//记录空格这个点 
                 } 

        //scanf不读取\n和空格
        int cnt = 0;


        while(~scanf("%c",&modol[cnt])) 
            if(modol[cnt]!='0')  cnt++;
            else break;
             /*
        char ch;
        while(scanf("%c",&ch))
            if(ch=='\n') continue;
            else if(ch==' ') continue;
            else if(ch!='0') {
                modol[cnt]=ch; 
                cnt++;
            }
            else break;
            */  
        //gets与scanf("%c")   getcahr 的区别 
        int flag=0, x1 = x , y1=y;
        modol[cnt] = 0;
        getchar();

        for(i=0;modol[i];i++){
            switch(modol[i]){
                case 'A' : x1 =  x - 1;y1=y; break;
                case 'B' : x1 =  x + 1;y1=y; break;
                case 'L' : x1 =  x;y1=y-1; break;
                case 'R' : x1 =  x;y1=y+1; break;
            }

            if(x1<0||y1<0||x1>4||y1>4){
                flag = 1;break;
            }else{
                a[x][y] = a[x1][y1];
                a[x1][y1] = ' ';
                x = x1;
                y = y1;
            }

        }

        if(cases++) printf("\n"); 
            printf("Puzzle #%d:\n",cases);

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



        }



    }


    return 0;
} 

你可能感兴趣的:(UVA)