请各位帮忙把这道题缩小解空间,!!!
国际象棋的棋盘位8 * 8的方格棋盘。现将“马”放在任意指定的方格中,按照“马”走棋的规则将“马”进行移动。要求每个方格只能进入一次,最终使得“马”走遍棋盘的64个方格。编写一个C程序,实现马踏棋盘操作,要求用1~64依次填入棋盘的方格中,并输出。0 0 0 0 0 0 0 0
0 0 # 0 # 0 0 0
0 # 0 0 0 # 0 0 ps: 1为马的第一个位置--初始(可任意) #为马可走的规则.
0 0 0 1 0 0 0 0
0 # 0 0 0 # 0 0
0 0 # 0 # 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
#include<stdio.h>
#define SIZE 8
int k,q;
int count = 1;
void Output(int (*x)[SIZE],int n)
{
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;printf("%6d",x[i][j++]));
puts("");
puts("");
}
}
int cmp(int (*x)[SIZE],int i,int j)
{
if(i > 7 || j > 7 || i < 0 || j < 0)
return 0;
if(x[i][j] == 0 && count != SIZE*SIZE && (i!=k || j!=q))
{
// printf("------------------------------------------------------------\n");
count++;
x[i][j] = count;
// Output(x,SIZE);
// printf("------------------------------------------------------------\n");
return 1;
}
else if(x[i][j] == 0 && count == SIZE*SIZE && i == k && j == q)
{
count++;
x[i][j] = count;
printf("最终:\n");
// Output(x,SIZE);
/// puts("--------------------------------------------------------------");
return -1;
}
else
{
return 0;
}
}
void fun(int (*x)[SIZE],int i,int j)
{
if(cmp(x,i,j) == -1)
return ;
if(cmp(x,i-2,j-1))
{
fun(x,i-2,j-1);
}
if(cmp(x,i-2,j+1))
{
fun(x,i-2,j+1);
}
if(cmp(x,i-1,j-2))
{
fun(x,i-1,j-2);
}
if(cmp(x,i-1,j+2))
{
fun(x,i-1,j+2);
}
if(cmp(x,i+1,j-2))
{
fun(x,i+1,j-2);
}
if(cmp(x,i+1,j+2))
{
fun(x,i+1,j+2);
}
if(cmp(x,i+2,j-1))
{
fun(x,i+2,j-1);
}
if(cmp(x,i+2,j+1))
{
fun(x,i+2,j+1);
}
count--;
x[i][j] = 0;
if(count == 0)
{
printf("\n此位置无解\n");
return ;
}
}
void main()
{
int vol[SIZE][SIZE];
int i =0,j = 0;
int n,m;
for(i =0;i<SIZE;i++)
for(j = 0;j<SIZE;vol[i][j++] = 0);
printf("请输出马的初始位置:");
scanf("%d %d",&n,&m);
printf("请输入马的最终位置:");
scanf("%d %d",&k,&q);
vol[n][m] = 1;
printf("初始状态 :");
puts("");
Output(vol,SIZE);
fun(vol,n,m);
printf("最终状态:");
puts("");
Output(vol,SIZE);
puts("");
}
以上第一种做法#define SIZE 8
int k,q;
int count = 1;
void Output(int (*x)[SIZE],int n)
{
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;printf("%6d",x[i][j++]));
puts("");
puts("");
}
}
int cmp(int (*x)[SIZE],int i,int j)
{
if(i > 7 || j > 7 || i < 0 || j < 0)
return 0;
if(x[i][j] == 0 && count != SIZE*SIZE && (i!=k || j!=q))
{
// printf("------------------------------------------------------------\n");
count++;
x[i][j] = count;
// Output(x,SIZE);
// printf("------------------------------------------------------------\n");
return 1;
}
else if(x[i][j] == 0 && count == SIZE*SIZE && i == k && j == q)
{
count++;
x[i][j] = count;
printf("最终:\n");
// Output(x,SIZE);
/// puts("--------------------------------------------------------------");
return -1;
}
else
{
return 0;
}
}
void fun(int (*x)[SIZE],int i,int j)
{
if(cmp(x,i,j) == -1)
return ;
if(cmp(x,i-2,j-1))
{
fun(x,i-2,j-1);
}
if(cmp(x,i-2,j+1))
{
fun(x,i-2,j+1);
}
if(cmp(x,i-1,j-2))
{
fun(x,i-1,j-2);
}
if(cmp(x,i-1,j+2))
{
fun(x,i-1,j+2);
}
if(cmp(x,i+1,j-2))
{
fun(x,i+1,j-2);
}
if(cmp(x,i+1,j+2))
{
fun(x,i+1,j+2);
}
if(cmp(x,i+2,j-1))
{
fun(x,i+2,j-1);
}
if(cmp(x,i+2,j+1))
{
fun(x,i+2,j+1);
}
count--;
x[i][j] = 0;
if(count == 0)
{
printf("\n此位置无解\n");
return ;
}
}
void main()
{
int vol[SIZE][SIZE];
int i =0,j = 0;
int n,m;
for(i =0;i<SIZE;i++)
for(j = 0;j<SIZE;vol[i][j++] = 0);
printf("请输出马的初始位置:");
scanf("%d %d",&n,&m);
printf("请输入马的最终位置:");
scanf("%d %d",&k,&q);
vol[n][m] = 1;
printf("初始状态 :");
puts("");
Output(vol,SIZE);
fun(vol,n,m);
printf("最终状态:");
puts("");
Output(vol,SIZE);
puts("");
}
#include<stdio.h>
#define X 8
#define Y 8
int chess[X][Y];
int nextxy(int *x,int *y,int count)
{
switch(count)
{
case 0:if(*x +2 <= X -1 && *y-1 >= 0 && chess[*x +2][*y-1] == 0)
{
*x = *x + 2;
*y = *y-1;
return 1;
}
break;
case 1:if(*x + 2<=X-1 && *y+1 <=Y-1 && chess[*x+2][*y+1] == 0)
{
*x = *x +2;
*y = *y + 1;
return 1;
}
break;
case 2:if(*x + 1<=X-1 && *y -2 >=0 && chess[*x +1 ][*y-2]==0)
{
*x = *x +1;
*y = *y - 2;
return 1;
}
break;
case 3:if(*x + 1 <= X -1 && *y +2 <= Y-1 && chess[*x+1][*y+2] == 0)
{
*x = *x +1;
*y = *y + 2;
return 1;
}
break;
case 4:if(*x -2>=0 && *y-1>= 0 && chess[*x -2][*y-1] == 0)
{
*x = *x -2;
*y = *y -1;
return 1;
}
break;
case 5:if(*x-2 >=0 && *y + 1<=Y-1 && chess[*x-2][*y+1] == 0)
{
*x = *x -2;
*y = *y +1;
return 1;
}
break;
case 6:if(*x -1 >= 0 && *y -2>=0 && chess[*x-1][*y-2] == 0)
{
*x = *x - 1;
*y = *y - 2;
return 1;
}
break;
case 7:if(*x - 1 >= 0 && *y+2<=Y-1 && chess[*x-1][*y+2] == 0)
{
*x = *x - 1;
*y = *y + 2;
return 1;
}
break;
default : break;
}
return 0;
}
int TravelChessBoard(int x,int y,int tag)
{
int x1 = x,y1 = y,flag = 0,count = 0;
chess[x][y] = tag;
if(tag == X * Y){return 1;}
flag = nextxy(&x1,&y1,count);
while(flag == 0 && count < 7)
{
count++;
flag = nextxy(&x1,&y1,count);
}
while(flag)
{
if(TravelChessBoard(x1,y1,tag + 1)) return 1;
x1 = x;y1 = y;
count++;
flag = nextxy(&x1,&y1,count);
while(flag == 0 && count < 7)
{
count++;
flag = nextxy(&x1,&y1,count);
}
}
if(flag == 0)
chess[x][y] = 0;
return 0;
}
void main()
{
int i,j;
for(i = 0;i<X;i++)
for(j = 0;j<Y;chess[i][j++] = 0);
if(TravelChessBoard(2,0,1))
{
for(i =0;i<X;i++)
{
for(j = 0;j<Y;printf("%6d",chess[i][j++]));
puts("");
puts("");
}
printf("The horse has travelled the chess borad\n");
}
else
printf("The horse cannot travel the chess board\n");
}
以上第二种。、#define X 8
#define Y 8
int chess[X][Y];
int nextxy(int *x,int *y,int count)
{
switch(count)
{
case 0:if(*x +2 <= X -1 && *y-1 >= 0 && chess[*x +2][*y-1] == 0)
{
*x = *x + 2;
*y = *y-1;
return 1;
}
break;
case 1:if(*x + 2<=X-1 && *y+1 <=Y-1 && chess[*x+2][*y+1] == 0)
{
*x = *x +2;
*y = *y + 1;
return 1;
}
break;
case 2:if(*x + 1<=X-1 && *y -2 >=0 && chess[*x +1 ][*y-2]==0)
{
*x = *x +1;
*y = *y - 2;
return 1;
}
break;
case 3:if(*x + 1 <= X -1 && *y +2 <= Y-1 && chess[*x+1][*y+2] == 0)
{
*x = *x +1;
*y = *y + 2;
return 1;
}
break;
case 4:if(*x -2>=0 && *y-1>= 0 && chess[*x -2][*y-1] == 0)
{
*x = *x -2;
*y = *y -1;
return 1;
}
break;
case 5:if(*x-2 >=0 && *y + 1<=Y-1 && chess[*x-2][*y+1] == 0)
{
*x = *x -2;
*y = *y +1;
return 1;
}
break;
case 6:if(*x -1 >= 0 && *y -2>=0 && chess[*x-1][*y-2] == 0)
{
*x = *x - 1;
*y = *y - 2;
return 1;
}
break;
case 7:if(*x - 1 >= 0 && *y+2<=Y-1 && chess[*x-1][*y+2] == 0)
{
*x = *x - 1;
*y = *y + 2;
return 1;
}
break;
default : break;
}
return 0;
}
int TravelChessBoard(int x,int y,int tag)
{
int x1 = x,y1 = y,flag = 0,count = 0;
chess[x][y] = tag;
if(tag == X * Y){return 1;}
flag = nextxy(&x1,&y1,count);
while(flag == 0 && count < 7)
{
count++;
flag = nextxy(&x1,&y1,count);
}
while(flag)
{
if(TravelChessBoard(x1,y1,tag + 1)) return 1;
x1 = x;y1 = y;
count++;
flag = nextxy(&x1,&y1,count);
while(flag == 0 && count < 7)
{
count++;
flag = nextxy(&x1,&y1,count);
}
}
if(flag == 0)
chess[x][y] = 0;
return 0;
}
void main()
{
int i,j;
for(i = 0;i<X;i++)
for(j = 0;j<Y;chess[i][j++] = 0);
if(TravelChessBoard(2,0,1))
{
for(i =0;i<X;i++)
{
for(j = 0;j<Y;printf("%6d",chess[i][j++]));
puts("");
puts("");
}
printf("The horse has travelled the chess borad\n");
}
else
printf("The horse cannot travel the chess board\n");
}
问题是。。我找不出减少解空间的方法。得出结果估计要几分钟,!!!
请各位帮忙给个可行的方法!!