You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
int main() {
char s[5][5];
char c;
int cnt = 0;
while (true) {
for (int i = 0; i < 5; i++) { //输入网格
for (int j = 0; j < 5; j++) {
scanf("%c", &s[i][j]);
if (s[0][0] == 'Z') return 0; //检查字母Z
}
getchar();
}
//找到空格的位置,用 i、j 记录
int i, j, flag = 0, flag2 = 0;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (s[i][j] == ' ') {
flag = 1;
break;
}
}
if (flag) break;
}
while ((c = getchar()) != '0') { //做出操作
if (c == 'A' && i - 1 >= 0) {
s[i][j] = s[i - 1][j];
s[i - 1][j] = ' ';
i -= 1; //更新空格的位置,下同
} else if (c == 'B' && i + 1 < 5) {
s[i][j] = s[i + 1][j];
s[i + 1][j] = ' ';
i += 1;
} else if (c == 'L' && j - 1 >= 0) {
s[i][j] = s[i][j - 1];
s[i][j - 1] = ' ';
j -= 1;
} else if (c == 'R' && j + 1 < 5) {
s[i][j] = s[i][j + 1];
s[i][j + 1] = ' ';
j += 1;
} else if (isspace(c)) continue; //防止空字符的干扰
else flag2 = 1; //指令非法
}
getchar(); //吸收指令行后的回车
if (!cnt) printf("Puzzle #%d:\n", ++cnt); //按照格式输出
else printf("\nPuzzle #%d:\n", ++cnt);
if (flag2) {
printf("This puzzle has no final configuration.\n");
continue;
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (!j) printf("%c", s[i][j]);
else printf(" %c", s[i][j]);
}
printf("\n");
}
}
}