在做这道题前就知道格式很坑,已经卡了三天(哭),希望大佬帮忙看看,不知道错哪,谢谢~
题目链接:UVA 227
注意第二个样例与第三个样例的puzzle要自己手动加上空格
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char c;
int m = 0;
char a[6][6];
int main(){
int x = 0,y = 0;
while(++m){
int f = 1;
for(int i = 0; i < 5; i++){//输入
for(int j = 0; j < 5; j++){
char c = getchar();
if(c == 'Z') return 0;
if(c == ' ') x = i,y = j;
if(c == '\n') {a[i][j] = ' ';continue;}
a[i][j] = c;
}
getchar();
}
if(m > 1) printf("\n");
printf("Puzzle #%d:\n",m);
char s;
while((s = getchar()) != '0'){//模拟
int b = x-1,c = y-1,d = x+1,e = y+1;
if(s == 'A' && x > 0)
swap(a[b][y],a[x--][y]);
else if(s == 'B' && x < 4)
swap(a[d][y],a[x++][y]);
else if(s == 'L' && y > 0)
swap(a[x][c],a[x][y--]);
else if(s == 'R' && y < 4)
swap(a[x][e],a[x][y++]);
else if(s == '\n'||s == ' ')
continue;
else{
printf("This puzzle has no final configuration.\n");
f = 0;
break;
}
}
getchar();//吸收'\n'
if(f){
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
if(j != 0) {
printf(" %c",a[i][j]);
}else{
printf("%c",a[i][j]);
}
}
putchar('\n');
}
}
}
return 0;
}