谜题问题,第二次输入后输出不对,请大佬指教。
#include<stdio.h>#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
int main()
{
while(1)
{
char str[5][5];
char order[100];
char c = '\0';
int si = 0, sj = 0;
memset(str, '\0', sizeof(str));
memset(order, '\0', sizeof(order));
int temp = 1;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
scanf("%c", &str[i][j]);
if (str[i][j] == ' ')
{
si = i;
sj = j;
}
if (str[i][j] == 'Z')
{
temp = 0;
break;
}
}
if (!temp) break;
c = getchar(); //读入,但是不用, 目的是跳过换行符
}
if (!temp) break;
int l = -1;
do
{
order[++l] = getchar(); //换行符也读入,后面再处理
} while (order[l] != '0');
/* for (int i = 0; i < strlen(order); i++)
printf("%c ", order[i]); */
int bo = 1;
for (int i = 0; i < l; i++)
{
if (order[i] == '\n') continue;
switch (order[i])
{
case 'A': {
if (si - 1 < 0) { bo = 0; break; }
str[si][sj] = str[si - 1][sj]; str[si - 1][sj] = ' '; si = si - 1;
}; break;
case 'B': {
if (si + 1 > 4) { bo = 0; break; }
str[si][sj] = str[si + 1][sj]; str[si + 1][sj] = ' '; si = si + 1;
}; break;
case 'R': {
if (sj + 1 > 4) { bo = 0; break; }
str[si][sj] = str[si][sj + 1]; str[si][sj + 1] = ' '; sj = sj + 1;
}; break;
case 'L': {
if (sj - 1 < 0) { bo = 0; break; }
str[si][sj] = str[si][sj - 1]; str[si][sj - 1] = ' '; sj = sj - 1;
}; break;
default:printf("moves wrong\n"); break;
}
if (bo == 0) break;
}
if (bo == 1)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
printf("%c ", str[i][j]);
}
printf("\n");
}
printf("\n");
}
else printf("This puzzle has no final configuration\n");
fflush(stdin);
}
system("pause");
}