题目:UVa--227
代码:
#include<stdio.h>
#include<string.h>
#define RE freopen("I:in.txt","r",stdin);
#define WE freopen("I:out.txt","w",stdout);
char a[5][5];
int main(){
int cases = 1;
int i , j , k , l;
// RE;
// WE;
while(gets(a[0])){
if(strcmp(a[0],"Z") == 0)
break;
gets(a[1]);
gets(a[2]);
gets(a[3]);
gets(a[4]);
char command[1000] = {'\0'} , subcommand[100]; //注意每一次都要将存放命令符号的数组清零
do{
gets(subcommand);
l = strlen(subcommand);
strcat(command,subcommand);
}while(subcommand[l-1]!='0'); //将不同行的命令符号放在同一数组中
int line , row;
for(i = 0;i < 5;i++)
for(j = 0;j < 5;j++)
if(a[i][j] == ' '){
line = i;
row = j;
break;
} //找出刚开始空格的位置
int length = strlen(command);
for(k = 0;k < length - 1;k++){
if(command[k] == 'A'){
if(line - 1 < 0)
break; //越界
else{
a[line][row] = a[line - 1][row];
a[line - 1][row] = ' ';
line--;
}
}
if(command[k] == 'B'){
if(line + 1 > 4)
break; //越界
else{
a[line][row] = a[line + 1][row];
a[line + 1][row] = ' ';
line++;
}
}
if(command[k] == 'L'){
if(row - 1 < 0)
break; //越界
else{
a[line][row] = a[line][row - 1];
a[line][row - 1] = ' ';
row--;
}
}
if(command[k] == 'R'){
if(row + 1 > 4)
break; //越界
else{
a[line][row] = a[line][row + 1];
a[line][row + 1] = ' ';
row++;
}
}
}
if(cases!=1)
printf("\n"); //相邻输出之间存在一空行
printf("Puzzle #%d:\n",cases++);
if(command[k] == '0'){
for(i = 0;i < 5;i++)
printf("%c %c %c %c %c\n",a[i][0],a[i][1],a[i][2],a[i][3],a[i][4]);
}
else
printf("This puzzle has no final configuration.\n");
}
return 0;
}