Files
python/RuMenJingDian/UVa/UVa227.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

63 lines
2.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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");
}
}
}