高手进来看啊!马的遍历补充修改下程序
#include"stdlib.h"#include"stdio.h"
struct Elem
{
char c;
int mark_direct[8];
};
Elem chessboard[13][14];
struct direct_increment
{
int dx;
int dy;
};
struct Postion
{
int x;
int y;
};
direct_increment direct_ay[8]={{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},
{-1,2}};
void init_chessboard(Elem chessboard[13][14])
{
int i,j,k;
for( i=0;i<13;i++)
for( j=0;j<14;j++)
(chessboard[i][j]).c='#';
for( i=2;i<11;i++)
for( j=2;j<12;j++)
{
(chessboard[i][j]).c='0';
for( k=0;k<8;k++)
((chessboard[i][j]).mark_direct)[k]=0;
}
}
bool check_postion(Postion pos)
{
if(pos.x<2||pos.x>9||pos.y<2||pos.y>9)return false;
else return true;
}
bool is_last_point(Elem chessboard[13][14],Postion pos)
{
char cc=chessboard[pos.x][pos.y].c;
if(cc=='1')return false;
else
for(int i=2;i<11;i++)
for(int j=2;j<12;j++)
if(chessboard[i][j].c=='0'&&!
(i==pos.x&&j==pos.y))return false;
return true;
}
int count_way(Postion pos)
{
int counter=0,i;
for( i=0;i<8;i++)
{
Postion pos1;
pos1.x=pos.x+direct_ay[i].dx;
pos1.y=pos.y+direct_ay[i].dy;
if((chessboard[pos.x][pos.y].mark_direct)[i]==0&&(chessboard
[pos1.x][pos1.y]).c=='0')
++counter;
}
return counter;
}
bool is_visited(Elem chessboard[13][14],Postion pos)
{
////
}
void mark_step(Elem chessboard[13][14],Postion pos)
{
////
}
bool Select_bestpost(Postion *ppos,Postion *bpos)
{
int ct;int i;int direct,direct_ct=8;
Postion pos1,pos_best;
int flag=0;
for( i=0;i<8;i++)
{
if(((chessboard[ppos->x][ppos->y]).mark_direct)[i]==0)
{
pos1.x=ppos->x+direct_ay[i].dx;
pos1.y=ppos->y+direct_ay[i].dy;
if(check_postion(pos1)&&!is_visited(chessboard,pos1))
if(is_last_point(chessboard,pos1))ct=1;
else
ct=count_way(pos1);
else ct=-1;
if(ct<=direct_ct&&ct>0)
{
pos_best.x=pos1.x;
pos_best.y=pos1.y;
flag=1;
direct_ct=ct;
direct=i;
}
}
else continue;
}
if(flag)
{
bpos->x=pos_best.x;
bpos->y=pos_best.y;
int reverse_direct=(direct)>4?direct-4:direct+4;
(chessboard[ppos->x][ppos->y]).mark_direct[direct]=1;
(chessboard[bpos->x][bpos->y]).mark_direct[reverse_direct]=1;
return true;
}
else
return false;
}
void clear_pos(Postion p,Postion next_pos)
{
/////
}
visit_complete(chessboard)
{
/////
}
bool Visit_Recursion(Elem chessboard[13][14],Postion pos)
{
Postion next_pos;
static Postion pos_beg=pos;
int Succ=0;static ct=0;
int i;
mark_step(chessboard,pos);
if(visit_complete(chessboard))
{
Succ=1;
ct=0;
return true;
}
else
for(i=0;!Succ&&i<8;i++)
{
if(!Select_bestpost(&pos,&next_pos))continue;
if(!Visit_Recursion(chessboard,next_pos))
{
clear_pos(chessboard,next_pos);
continue;
}
else Succ=1;
++ct;
printf("<=%d:%d",next_pos.x,next_pos.y);
if(ct%8==0)printf("\n");
}
if(!Succ)return false;
}
void main()
{
Postion pos1;
char ch;
init_chessboard(chessboard);
while(1)
{
printf("请输入马在棋盘中的起始位置,可以随便设(2<=x<=10,2<=y<=11
)\n");
printf("输入的两个位置用空格分割,用ENTER键确认\n");
scanf("%d%d",&pos1.x,&pos1.y);
if(Visit_Recursion(chessboard,pos1))
{
printf("<=%d:%d\n",pos1.x,pos1.y);
printf("逆向打印走过的路径\n");
printf("遍历结束!\n");
}
else
printf("没有满足条件的路径!\n");
printf("还要试一下其它起始位置吗?输入Y继续,输入其它字符退出\n");
scanf("%c",ch);
init_chessboard(chessboard);
if(ch=='y')continue;
else break;
}
}