Puzzle
这个题就是我们小时候玩的拼图。
我感觉只要把那些存在的的情况考虑到了一般没问题。
#include <stdio.h>
#include <string.h>
int main(){
char a[10][10],b[1010];
int t=0,x,y,x1,y1,flag;
while(gets(a[0])){
if (a[0][0]=='Z'){
break;
}
x=0;y=0;
for (int i=1;i<5;i++){
gets(a[i]);
}
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
if (a[i][j]==' '){
x=i;
y=j;
break;
}
}
}
int xx=0;
while(~scanf("%c",&b[xx])){
if (b[xx]!='0'){
xx++;
}
else{
break;
}
}
b[xx]='\0';
getchar();
flag=0;
for (int i=0;b[i];i++){
if (b[i]=='A'){
x1=x-1;
y1=y;
}
else if (b[i]=='B'){
x1=x+1;
y1=y;
}
else if (b[i]=='L'){
x1=x;
y1=y-1;
}
else if (b[i]=='R'){
x1=x;
y1=y+1;
}
if (x1<0||x1>4||y1<0||y1>4){
flag=1;
break;
}
else{
a[x][y]=a[x1][y1];
a[x1][y1]=' ';
x=x1;
y=y1;
}
}
if (t++){
printf("\n");
}
printf("Puzzle #%d:\n",t);
if (flag){
printf("This puzzle has no final configuration.\n");
}
else{
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
printf("%c%c",a[i][j],j!=4?' ':'\n');
}
}
}
}
return 0;
}