https://vjudge.net/problem/UVA-227
ps:此题格式很重要多次PE;
#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char maze[8][8];
int x,y,flag;
void up(){
if(x-1>=1){
maze[x][y]=maze[x-1][y];
x--;
maze[x][y]=' ';
}
else{
flag=1;
printf("This puzzle has no final configuration.\n");
}
}
void down(){
if(x+1<=5){
maze[x][y]=maze[x+1][y];
x++;
maze[x][y]=' ';
}
else{
flag=1;
printf("This puzzle has no final configuration.\n");
}
}
void le(){
if(y-1>=1){
maze[x][y]=maze[x][y-1];
y--;
maze[x][y]=' ';
}
else{
flag=1;
printf("This puzzle has no final configuration.\n");
}
}
void ri(){
if(y+1<=5){
maze[x][y]=maze[x][y+1];
y++;
maze[x][y]=' ';
}
else{
flag=1;
printf("This puzzle has no final configuration.\n");
}
}
int main()
{
int kase=0;
while(true){
flag=0;
for(int i=1; i<=5; i++) {
for(int j=1;j<=5;j++){
scanf("%c",&maze[i][j]);
if(maze[i][j]=='Z')
exit(0);
if(maze[i][j]==' '){
x=i;y=j;
}
}
getchar();//吸收每行的换行
}
int indx=0;
char c[1000],ty;
while((ty=getchar())!='0'){ //指令只有遇到0才停
if(ty=='A'||ty=='B'||ty=='L'||ty=='R')//排除空格,换行等
c[indx++]=ty;
}
if(!kase)
printf("Puzzle #%d:\n",++kase);//格式
else printf("\nPuzzle #%d:\n",++kase);
//int len = strlen(c);
for(int i = 0; i < indx ;i++){
if(flag)break;
if(c[i]=='A')up();
else if(c[i]=='B')down();
else if (c[i]=='L') le();
else ri();
}
if(!flag){
for(int i=1; i<=5; i++) {
for(int j=1;j<=5;j++){
printf("%c",maze[i][j]);
if(j!=5)
printf(" ");
}
printf("\n");
}
}
getchar();//吸收最后的换行
}
return 0;
}
/*
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z
*/