Uva227 谜题

题目很简单跟着题意写就行了,但重要的是!!!格式,被格式卡了好久嫩麻,格式注意以下几点:

  1. 一行中最后一个字符为空时,如测试案例2,后面的空格需要自己补

  1. 遇到换行和空格的处理,用getchar()最好每一个字符都读取,防止少读。

  1. 多个案例的输出,第一个前没有换行,最后一个后没有换行

当我好不容易处理了所有的输入输出以及格式后发现还是WA,抓毛了一个小时全网搜测试案例又发现了一个坑点测试用例,当输入的操作不为ABLR的处理,因为只有操作越界才会输出谜题无解,当遇到非法输入时忽略不对其做处理即可。

测试案例如下

当输入为:

AAAAA

BBBBB

NNNNN

JJJJJ

UUUU

AQ

0

Z

输出:

然后终于AC了,哭哭惹,这题真太变态了,不过经过此题,确实扎实了对输入处理的基础,还是很好的锻炼到了自己。

AC代码如下,感觉我写的代码很拉跨,还可以优化emmm

#include<iostream>
#include<cstring>
using namespace std;
char a[10][10];
int x=0,y=0;
bool judge(int x) {
    if(x>=1&&x<=5)
        return true;
    return false;
}
int main() {
    char c;
    int cnt=0,flag=1;
    while(true) {
        for(int i=1; i<=5; i++) {
            for(int j=1; j<=5; j++) {
                if(c!='Z'&&(c=getchar())!='Z') {
                    if(c=='\n') {
                        if(j==5)//空格在一行最后补上
                            c=' ';
                        else
                            c=getchar();
                    }//如果遇到换行符读取下一个
                    a[i][j]=c;
                    if(a[i][j]==' ') {
                        x=i;
                        y=j;
                    }
                } else
                    return 0;
            }
        }
//        for(int i=1; i<=5; i++) {
//            for(int j=1; j<=5; j++) {
//                if(j==5)
//                    cout<<a[i][j];
//                else
//                    cout<<a[i][j]<<" ";
//            }
//            cout<<endl;
//        }
        cnt++;
        while((c=getchar())!=EOF&&c!='0') {
            if(c=='A'||c=='B'||c=='L'||c=='R') {
                if(c=='A'&&judge(x-1)) {
                    swap(a[x][y],a[x-1][y]);
                    x=x-1;
                } else if(c=='B'&&judge(x+1)) {
                    swap(a[x][y],a[x+1][y]);
                    x=x+1;
                } else if(c=='L'&&judge(y-1)) {
                    swap(a[x][y],a[x][y-1]);
                    y=y-1;
                } else if(c=='R'&&judge(y+1)) {
                    swap(a[x][y],a[x][y+1]);
                    y=y+1;
                } else if(c=='Z')
                    return 0;
                else if(c=='\n')
                    continue;
                else {
                    //cout<<"This puzzle has no final configuration."<<endl;
                    flag=0;
                }
            } else
                continue;

        }
        if(cnt!=1)
            cout<<endl;
        if(flag) {
            cout<<"Puzzle #"<<cnt<<":"<<endl;
            for(int i=1; i<=5; i++) {
                for(int j=1; j<=5; j++) {
                    if(j==5)
                        cout<<a[i][j];
                    else
                        cout<<a[i][j]<<" ";
                }
                cout<<endl;
            }
        } else {
            cout<<"Puzzle #"<<cnt<<":"<<endl;
            cout<<"This puzzle has no final configuration."<<endl;
            flag=1;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值