今天来一道有些难度的题吧!
题目:数字移动
在图中的九个点上,空出中间的点,其余的点上任意填入数字1至8;1的位置固定不动,然后移动其余的数字,使1到8顺时针从小到大排列。移动的规则是:只能将数字沿线移向空白的点。
程序如下:
#include <stdio.h>
int a[] = {
0, 1, 2, 5, 8, 7, 6, 3};
int b[9];
int c[9];
int count = 0;
int main()
{
int i, j, k, t;
void print();
printf("Please enter the numbers: ");
for (i = 0; i < 8; i++)
{
scanf("%d", &b[a[i]]);
}
print();
for (t = -1, j = 0; j < 8 && t == -1; j++)
{
if (b[a[j]] == 1)
{
t = j;
}
}
for (j = 0; j < 8; j++)
{
c[j] = a[(j + t) % 8];
}
for (i = 2; i < 9; i++)
{
for (j = i - 1; j < 8; j++)
{
if (b[c[j]] == i && j != i - 1)
{
b[4] = j;
b[c[j]] = 0;
print();
for (k = j; k != i - 1; k--)
{
b[c[k]] = b[c[k - 1]];
b[c[k - 1]] = 0;
print();
}
b[c[k]] = i;
b[4] = 0;
print();
break;
}
else if (b[c[j]] == i)
{
break;
}
}
}
return 0;
}
void print(void)
{
int c;
for (c = 0; c <