马的遍历执行过程中 需要输入可执行文件 不懂 程序如下
马的遍历执行过程中 需要输入可执行文件 不懂 请指教 程序如下
struct Elem
{ char c;
int mark_direct[8];
};
Elem chessboard[12][12];
struct direct_increment
{ int dx;
int dy;
};
struct Postion
{ int x;
int y;
};
direct_increment direct_ar[8]={{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
void init_chessboard(Elem(*p)[12])
{
for(int i=0;i<12;i++)
for(int j=0;j<12;j++)
p[i][j].c='#';
for(i=2;i<9;i++)
for(int j=2;j<=9;j++)
{ (p[i][j]).c='0';
for(int k=0;k<8;k++)
((p[i][j]).mark_direct[k])[k]=0;
}
}
bool check_postion(Postion pos)
{
if(pos.x<2||pos.x>9||pos.y<2||pos.y>9)return false;
else return ture;
}
bool is_last_point(Elem(*P)[12],Postion pos)
{
char cc=p[pos.x][pos.y].c;
if(cc=='1')return false;
else
for(int i=2;i<=9;i++)
for(int j=2;j<=9;j++)
if(p[i][j].c=='0'&&!(i==pos.x&&pos.y)) return false;
return true;
}
int count_way(Postion pos)
{
int counter=0;
for(int i=0;i<8;i++)
{
Poation 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[pos.x+direct_ay[i].dx][pos.y+direct_ay[i].dy]).c=='0')
++counter;
}
return counter;
}
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(chesssboard,pos1))
if(is_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[ppos->x][ppos->y]).mark_direct[reverse_direct]=1;
return true;
}
else
return false;
}
bool Visit_Recursion(Elem(*p)[12],Postion pos)
{ Postion next_pos;
static Poation 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(p,ext_pos))
{
clear_pos(p,next_pos);
continue;
}
else Succ=1;
++ct;
printf("<=%d:%d",next_pos.x,next_pos.y);
if(ct%8==0)printf("\n");
}
void main()
{ Postion pos1;
char ch;
init_chessboard(chessboard);
while(1)
{
printf("输入马在棋盘中的起始位置,可以随便设置(2<=x<=9,2<=y<=9)\n");
printf("输入的两个位置用空格分隔,用Enter键确认\n");
scanf("%d%d",&pos1.x,&pos1.y);
if(Visit_Recursion(chessboard,pos1))
{
printf("<=%d:%d\n",pos1.x,pos1.y);
struct Elem
{ char c;
int mark_direct[8];
};
Elem chessboard[12][12];
struct direct_increment
{ int dx;
int dy;
};
struct Postion
{ int x;
int y;
};
direct_increment direct_ar[8]={{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
void init_chessboard(Elem(*p)[12])
{
for(int i=0;i<12;i++)
for(int j=0;j<12;j++)
p[i][j].c='#';
for(i=2;i<9;i++)
for(int j=2;j<=9;j++)
{ (p[i][j]).c='0';
for(int k=0;k<8;k++)
((p[i][j]).mark_direct[k])[k]=0;
}
}
bool check_postion(Postion pos)
{
if(pos.x<2||pos.x>9||pos.y<2||pos.y>9)return false;
else return ture;
}
bool is_last_point(Elem(*P)[12],Postion pos)
{
char cc=p[pos.x][pos.y].c;
if(cc=='1')return false;
else
for(int i=2;i<=9;i++)
for(int j=2;j<=9;j++)
if(p[i][j].c=='0'&&!(i==pos.x&&pos.y)) return false;
return true;
}
int count_way(Postion pos)
{
int counter=0;
for(int i=0;i<8;i++)
{
Poation 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[pos.x+direct_ay[i].dx][pos.y+direct_ay[i].dy]).c=='0')
++counter;
}
return counter;
}
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(chesssboard,pos1))
if(is_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[ppos->x][ppos->y]).mark_direct[reverse_direct]=1;
return true;
}
else
return false;
}
bool Visit_Recursion(Elem(*p)[12],Postion pos)
{ Postion next_pos;
static Poation 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(p,ext_pos))
{
clear_pos(p,next_pos);
continue;
}
else Succ=1;
++ct;
printf("<=%d:%d",next_pos.x,next_pos.y);
if(ct%8==0)printf("\n");
}
void main()
{ Postion pos1;
char ch;
init_chessboard(chessboard);
while(1)
{
printf("输入马在棋盘中的起始位置,可以随便设置(2<=x<=9,2<=y<=9)\n");
printf("输入的两个位置用空格分隔,用Enter键确认\n");
scanf("%d%d",&pos1.x,&pos1.y);
if(Visit_Recursion(chessboard,pos1))
{
printf("<=%d:%d\n",pos1.x,pos1.y);