227 - Puzzle

这题太坑了,所有数据都对,就是WA,放了好久,后来才知道是数据间的空行的原因。

各组数据之间要有空行,最前和最后都不能有空行,不然就是WA

在uva上只有空格的错误才是PE

#include<stdio.h>
#include<string.h>
char a[20][20];
int main()
{
    int i,j,maxn=1,ens=0;
    while(gets(a[0])!=NULL)
    {
        if(a[0][0]=='Z')
            return 0;
            int x,y;
        for(i=1;i<5;i++)
            gets(a[i]);
        for(i=0;i<5;i++){
            for(j=0;j<5;j++)
            {
                if(a[i][j]==' ')
                {
                    x=i;y=j;//表示空格的地址
                }
            }
        }
            int tot=1,c;//tot为非法操作的判别标志
            while((c=getchar())!='0')
            {
                if(tot&&c=='A')
                {
                    if(x>0){
                    a[x][y]=a[x-1][y];a[x-1][y]=' ';x-=1;
                }
                else
                    tot=0;
                }
                else if(tot&&c=='B')
                {
                    if(x<4){
                    a[x][y]=a[x+1][y];a[x+1][y]=' ';x+=1;
                }
                else
                    tot=0;
                }
                else if(tot&&c=='L')
                {
                    if(y>0){
                    a[x][y]=a[x][y-1];a[x][y-1]=' ';y-=1;
                }
                else
                    tot=0;
                }
                else if(tot&&c=='R')
                {
                    if(y<4){
                    a[x][y]=a[x][y+1];a[x][y+1]=' ';y+=1;
                }
                else
                    tot=0;
                }
                else if (tot && c != '\n') {
                tot=0;
            } else continue;
            }
            getchar();//吃掉缓冲区的换行符
            if(ens==0)
                ens=1;
            else
                printf("\n");
            printf("Puzzle #%d:\n",maxn++);
            if(tot==0)
                printf("This puzzle has no final configuration.\n");
            else{
            for(i=0;i<5;i++){
                for(j=0;j<5;j++){
                if(j==4)
                    printf("%c",a[i][j]);
                else
                    printf("%c ",a[i][j]);
            }
            printf("\n");
            }
            }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值