这道题目还是不难的,但是要注意gcc里面gets已经不能用了,用gets_s还是可以的,尽管我并不知道有什么区别
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char a[20][20]; int x, y; char s[1000];
int blank_x = 0, blank_y = 0; int flag = 1;
gets_s(a[0]);
gets_s(a[1]);
gets_s(a[2]);
gets_s(a[3]);
gets_s(a[4]);
/*for (x = 0; x < 5; x++)
{
for (y = 0; y < 5; y++)
{
scanf("%c", &a[x][y]);
}
}*/
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
if (a[x][y] = ‘ ‘)
{
blank_x = x;
blank_y = y;
}
}
}
gets_s(s);
int len = strlen(s);
for (int i = 0; i < len; i++)
{
if (s[len - 1] == ‘0‘ || s[i] == ‘A‘ || s[i] == ‘R‘ || s[i] == ‘B‘ || s[i] == ‘L‘)
{
if (s[i] == ‘A‘&&blank_x != 0)
{
a[blank_x][blank_y] = a[blank_x - 1][blank_y];
a[blank_x - 1][blank_y] = ‘ ‘;
}
if (s[i] == ‘R‘&&blank_y != 4)
{
a[blank_x][blank_y] = a[blank_x][blank_y + 1];
a[blank_x][blank_y + 1] = ‘ ‘;
}
if (s[i] == ‘B‘&&blank_x != 4)
{
a[blank_x][blank_y] = a[blank_x + 1][blank_y];
a[blank_x + 1][blank_y] = ‘ ‘;
}
if (s[i] == ‘L‘&&blank_y != 0)
{
a[blank_x][blank_y] = a[blank_x][blank_y - 1];
a[blank_x][blank_y - 1] = ‘ ‘;
}
}
else flag = 0;
}
if (flag)
{
for (int i = 0; i < 5; i++)
{
printf("%c %c %c %c %c\n", a[i][0], a[i][1], a[i][2], a[i][3], a[i][4]);
}
}
else printf("the puzzle has no final configuration!\n");
system("pause");
}