这题太坑了,所有数据都对,就是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;
}