# 马踏棋盘问题

## 回溯法

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MATRIX 8
#define TRUE 1
#define ERROR 0
typedef int status ;

int tag = 1;
/*查找下一步方向*/
status nextStep(int (*chess)[MATRIX],int *x,int *y,int step){
switch(step)
{
case 0:
if(*y+2<=MATRIX-1 && *x-1>=0 && chess[*x-1][*y+2]==0)
{
*y+=2;
*x-=1;
return TRUE;
}
break;
case 1:
if(*y+2<=MATRIX-1 && *x+1<=MATRIX-1 && chess[*x+1][*y+2]==0)
{
*y+=2;
*x+=1;
return TRUE;
}
break;
case 2:
if(*y+1<=MATRIX-1 && *x+2<=MATRIX-1 && chess[*x+2][*y+1]==0)
{
*y+=1;
*x+=2;
return TRUE;
}
break;
case 3:
if(*y-1>=0 && *x+2<=MATRIX-1 && chess[*x+2][*y-1]==0)
{
*y-=1;
*x+=2;
return TRUE;
}
break;
case 4:
if(*y-2>=0 && *x+1<=MATRIX-1 && chess[*x+1][*y-2]==0)
{
*y-=2;
*x+=1;
return TRUE;
}
break;
case 5:
if(*y-2>=0 && *x-1>=0 && chess[*x-1][*y-2]==0)
{
*y-=2;
*x-=1;
return TRUE;
}
break;
case 6:
if(*y-1>=0 && *x-2>=0 && chess[*x-2][*y-1]==0)
{
*y-=1;
*x-=2;
return TRUE;
}
break;
case 7:
if(*y+1<=MATRIX-1 && *x-2>=0 && chess[*x-2][*y+1]==0)
{
*y+=1;
*x-=2;
return TRUE;
}
break;
default:
break;
}
return ERROR;

}

/*DFS棋盘*/
void traverseChess(int (*chess)[MATRIX],int x,int y){

int count = 0;  //最多8个方向计数
int x1 = x,y1 = y;
if(x1<MATRIX && y1<MATRIX && chess[x1][y1] == 0){
chess[x1][y1] = tag;
tag ++;
}

if(tag > MATRIX * MATRIX){
return;
}

for(count = 0;count<8;count++){  //总共有8个可能的方向
nextStep(chess,&x1,&y1,count);
if(chess[x1][y1]==0)traverseChess(chess,x1,y1);  //递归
}
}
/*打印棋盘*/
void print_chess(int (*chess)[MATRIX]){
int i = 0,j = 0;
printf("\nThe MATRIX is as follow\n");
for(i = 0;i<MATRIX;i++){
for(j = 0;j < MATRIX;j++){
printf("%2d\t",chess[i][j]);
}
printf("\n");
}
}
void main(){

int i ,j;
clock_t start ,end;
int chess[MATRIX][MATRIX];
for(i = 0; i<MATRIX;i++){
for(j = 0;j < MATRIX;j++){
chess[i][j] = 0;  //初始化
}
}
start = clock();
traverseChess(chess,4,0,1);  //开始遍历
end = clock();
print_chess(chess);  //打印棋盘
printf("共耗时:%f\n",(double)(end-start)/CLOCKS_PER_SEC);

}


## 贪心算法

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define ROWS    8
#define COLS    8

/*cursor i move chess*/
const int iktmove[8] = {-2,-1,1,2,2,1,-1,-2};
/*cursor j move chess*/
const int jktmove[8] = {1,2,2,1,-1,-2,-2,-1};

int chess[ROWS][COLS];

/*inital the chess*/
void chess_init(int chess[][COLS],int rows,int cols)
{
int i, j;
for(i = 0; i < rows ; ++ i)
for (j = 0; j < cols ; ++ j)
{
chess[i][j] = 0;
}
}
/*print the chess*/
void print_chess(int chess[][COLS])
{
int i,j;
for(i = 0; i < ROWS; ++ i)
{
for(j = 0; j < COLS; ++ j)
printf("%d\t",chess[i][j]);
printf("\n\n");
}
}
/*find the index of min non-zeros*/
int minindex(int a[],int cols)
{
int i = 0,index = 0;
int min = a[0];
for(i = 0 ; i< cols; ++ i)
{
if(a[i] >0)
{
min = a[i];
index = i;
break;
}
}
for(i = index + 1; i < cols ; ++ i)
{
if(a[i] > 0 && min > a[i])
{
min = a[i];
index = i;
}
}
if(a[index] > 0)
return index;
return -1;
}

/**/
void greedy_chess(int chess[][COLS],int rows,int cols,int start_i,int start_j)
{
int i,npos,m,min,j,nnpos;

int nexti[8] = {0,0,0,0,0,0,0,0};
int nextj[8] = {0,0,0,0,0,0,0,0};
int exit[8] = {0,0,0,0,0,0,0,0};

/*init*/
chess_init(chess,rows,cols);
/*first step*/
chess[start_i][start_j] = 1;
/*n>1 step*/
for(m = 1; m < 64; ++ m)
{
/*steps d*/
npos = 0;
for(i = 0; i < 8; ++ i)
{
/*ignore the point which doesn't satisfy the conditions*/
if( start_i + iktmove[i] < 0 ||
start_i + iktmove[i] >= rows ||
start_j + jktmove[i] < 0 ||
start_j + jktmove[i] >= cols ||
chess[start_i + iktmove[i]][start_j+jktmove[i]] > 0)
{
continue;
}
/*save the point which satisfy the conditions*/
nexti[npos] = start_i + iktmove[i];
nextj[npos] = start_j + jktmove[i];
/*statistics how many point satisfy conditions*/
npos ++;
}
/*steps e*/
if(npos == 0)
{
printf("Can not finish the game!!\n,The steps of game can be %d\n",m);
return;
}
/*steps e*/
if(npos == 1)
{
min = 0;
start_i = nexti[min];
start_j = nextj[min];
chess[start_i][start_j] = m+1;
}
/*steps f*/
if(npos > 1)
{
for(i = 0; i < npos ; ++ i)
{
nnpos = 0;
for(j = 0; j < 8; ++ j)
{
if( nexti[i] + iktmove[j] >= 0 &&
nexti[i] + iktmove[j] < rows &&
nextj[i] + jktmove[j] >= 0 &&
nextj[i] + jktmove[j] < cols &&
chess[nexti[i] + iktmove[j]][nextj[i] + jktmove[j]] == 0)
{
nnpos ++;
}
}
exit[i] = nnpos;
}
if((min = minindex(exit,npos)) >= 0)
{
start_i = nexti[min];
start_j = nextj[min];
chess[start_i][start_j] = m+1;
}
else
{
printf("Can not finish the game!!\n,The steps of game can be %d\n",m);
return;
}
}
}
}

void main()
{
clock_t start,end;
start = clock();
greedy_chess(chess,ROWS,COLS,5,1);
end = clock();
print_chess(chess);
printf("time consumed:%f\n",(double)(end-start)/CLOCKS_PER_SEC);

}
• 点赞 2
• 评论 5
• 分享
x

海报分享

扫一扫，分享海报

• 收藏 9
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

红叶幽香

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
07-25 1376