|
楼主 |
发表于 2007-12-22 02:41:30
|
显示全部楼层
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// free=0, e=1, w=2, n=3, s=4, t=5, b=6
// move e(right)=1, w(left)=2, n(up)=3, s(down)=4
int position(int mp, int direction)
{
short int n[ 6][ 4] = {6, 5, 1, 1, 5, 6, 2, 2, 3, 3, 6, 5, 4, 4, 5, 6, 1, 2, 3, 4, 2, 1, 4, 3};
return n[ mp - 1][ direction - 1];
}
#define MM 1000
short int t[ MM][ 8][ 8], u[ MM][ 8][ 8], v[ 8][ 8], ti[ MM], tj[ MM];
int main (void)
{
int i, j, k, n, n_new, mp, mp_new;
n = 0;
ti[ n] = tj[ n] = 0;
for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) t[ n][ i][ j] = u[ n][ i][ j] = 0;
t[ 0][ 0][ 0] = 5;
L_0:
n_new = n;
mp = t[ n][ ti[ n]][ tj[ n]];
// right
mp_new = position(mp, 1);
if (tj[ n] < 7) if (!t[ n][ ti[ n]][ tj[ n] + 1]) if (mp_new != 5)
{
n_new++;
for (i = 0; i < 8; i++) for (j = 0; j < 8; j++)
{
t[ n_new][ i][ j] = t[ n][ i][ j];
u[ n_new][ i][ j] = u[ n][ i][ j];
}
ti[ n_new] = ti[ n];
tj[ n_new] = tj[ n] + 1;
t[ n_new][ ti[ n_new]][ tj[ n_new]] = mp_new;
u[ n_new][ ti[ n_new]][ tj[ n_new]] = 1;
}
// left
mp_new = position(mp, 2);
if (tj[ n]) if (!t[ n][ ti[ n]][ tj[ n] - 1]) if (mp_new != 5 || (ti[ n] == 7 && tj[ n] == 1))
{
n_new++;
for (i = 0; i < 8; i++) for (j = 0; j < 8; j++)
{
t[ n_new][ i][ j] = t[ n][ i][ j];
u[ n_new][ i][ j] = u[ n][ i][ j];
}
ti[ n_new] = ti[ n];
tj[ n_new] = tj[ n] - 1;
t[ n_new][ ti[ n_new]][ tj[ n_new]] = mp_new;
u[ n_new][ ti[ n_new]][ tj[ n_new]] = 2;
}
// upword
mp_new = position(mp, 3);
if (ti[ n]) if (!t[ n][ ti[ n] - 1][ tj[ n]]) if (mp_new != 5)
{
n_new++;
for (i = 0; i < 8; i++) for (j = 0; j < 8; j++)
{
t[ n_new][ i][ j] = t[ n][ i][ j];
u[ n_new][ i][ j] = u[ n][ i][ j];
}
ti[ n_new] = ti[ n] - 1;
tj[ n_new] = tj[ n];
t[ n_new][ ti[ n_new]][ tj[ n_new]] = mp_new;
u[ n_new][ ti[ n_new]][ tj[ n_new]] = 3;
}
// downword
mp_new = position(mp, 4);
if (ti[ n] < 7) if (!t[ n][ ti[ n] + 1][ tj[ n]]) if (mp_new != 5 || (ti[ n] == 6 && tj[ n] == 0))
{
n_new++;
for (i = 0; i < 8; i++) for (j = 0; j < 8; j++)
{
t[ n_new][ i][ j] = t[ n][ i][ j];
u[ n_new][ i][ j] = u[ n][ i][ j];
}
ti[ n_new] = ti[ n] + 1;
tj[ n_new] = tj[ n];
t[ n_new][ ti[ n_new]][ tj[ n_new]] = mp_new;
u[ n_new][ ti[ n_new]][ tj[ n_new]] = 4;
}
if (n_new > n)
{
for (k = n; k < n_new; k++) for (i = 0; i < 8; i++) for (j = 0; j < 8; j++)
{
t[ k][ i][ j] = t[ k + 1][ i][ j];
u[ k][ i][ j] = u[ k + 1][ i][ j];
ti[ k] = ti[ k + 1];
tj[ k] = tj[ k + 1];
}
n = n_new - 1;
if (n > MM - 4)
{
printf("not finifshed\n");
return 0;
}
goto L_0;
}
for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) if (!t[ n][ i][ j])
{
n--;
if (n >= 0) goto L_0;
else return 0;
}
if (ti[ n] == 7 && tj[ n] == 0) // && t[ n][ i][ j] == 5)
{
for (i = 0; i < 8; i++) for (j = 0; j < 8; j++)
{
if (u[ n][ i][ j] == 1) v[ i][ j - 1] = 1;
else if (u[ n][ i][ j] == 2) v[ i][ j + 1] = 2;
else if (u[ n][ i][ j] == 3) v[ i + 1][ j] = 3;
else if (u[ n][ i][ j] == 4) v[ i - 1][ j] = 4;
}
v[ ti[ n]][ tj[ n]] = 0;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++) printf("%d-%d ", t[ n][ i][ j], v[ i][ j]);
printf("\n");
}
printf("\n");
}
n--;
if (n >= 0) goto L_0;
return 0;
} |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
×
|