题目很简单跟着题意写就行了,但重要的是!!!格式,被格式卡了好久嫩麻,格式注意以下几点:
一行中最后一个字符为空时,如测试案例2,后面的空格需要自己补
遇到换行和空格的处理,用getchar()最好每一个字符都读取,防止少读。
多个案例的输出,第一个前没有换行,最后一个后没有换行
当我好不容易处理了所有的输入输出以及格式后发现还是WA,抓毛了一个小时全网搜测试案例又发现了一个坑点测试用例,当输入的操作不为ABLR的处理,因为只有操作越界才会输出谜题无解,当遇到非法输入时忽略不对其做处理即可。
测试案例如下
当输入为:
AAAAA
BBBBB
NNNNN
JJJJJ
UUUU
AQ
0
Z
输出:
![](https://img-blog.csdnimg.cn/img_convert/dd758ad417632df35c887b3b08f632da.png)
然后终于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;
}