题目链接如下:
这道题其实没有很难,但有一个巨坑的点,就是我估计,原题的格式是符合题意的,就算一行中第五个字符是' ',照样是给出来的;但洛谷给出的样例,最后一个字符是' ',直接是跳过的。所以,能被uva AC的代码,跑洛谷的样例是错误的......就因为这,我在这道题上耗费了好几个钟头......
我的AC代码如下:
cpp
#include <cstdio>
int u, v, kase;
char ch;
char a[5][5];
bool flag = true;
int main(){
kase = 0;
while(scanf("%c", &ch) == 1 && ch != 'Z'){
a[0][0] = ch;
if(a[0][0] == ' '){
u = v = 0;
}
for(int i = 0; i < 5; ++i){
for(int j = (i == 0 ? 1 : 0); j < 5; ++j){
scanf("%c", &a[i][j]);
if(a[i][j] == ' '){
u = i;
v = j;
}
}
getchar();
}
flag = true;
while(scanf("%c", &ch) == 1 && ch != '0'){
if(!flag){
continue;
}
if(ch == 'A'){
if(u == 0){
flag = false;
} else{
a[u][v] = a[u - 1][v];
u--;
}
} else if(ch == 'B'){
if(u == 4){
flag = false;
} else{
a[u][v] = a[u + 1][v];
u++;
}
} else if(ch == 'L'){
if(v == 0){
flag = false;
} else{
a[u][v] = a[u][v - 1];
v--;
}
} else if(ch == 'R'){
if(v == 4){
flag = false;
} else{
a[u][v] = a[u][v + 1];
v++;
}
}
}
getchar();
printf("%s", kase == 0 ? "" : "\n");
printf("Puzzle #%d:\n", ++kase);
if(!flag){
printf("This puzzle has no final configuration.\n");
continue;
}
a[u][v] = ' ';
for(int i = 0; i < 5; ++i){
for(int j = 0; j < 5; ++j){
printf("%c%s", a[i][j], j == 4 ? "\n" : " ");
}
}
}
return 0;
}