该楼层涉嫌违规被系统折叠隐藏这栋楼,查看这栋楼
没想到晚上这么多人,谢谢回复!
各位前辈
这是程序的源代码,不应该太长,所需的数据基本上已经相应了printf(16进制)了
可直接运行,
malloc_c是用来计数malloc使用起乘以的空间sizeof(trl)即是占用的byte的数量
棋盘是 7格,第一步是-1,
#include
#include
#include
#define side 7
typedef struct{
int i;
int j;
} stp;
struct trl;
struct trl{
int tbl[side*side]; // 1 for tred slots
stp cur_stp;
struct trl *next;
};
typedef struct trl trl;
trl trl_init={.cur_stp={0,0},.tbl={-1},.next=NULL};
trl * head;
stp vld_stps[8]; /*steps are represented in absolute coordinates on the tbl, all negative values are invalid*/
void stp_gen(trl * trl);//generate possible steps, filter out those beyond table,put the remaining in vld_stps; the first vacuu***ot will be given two negative values for termination.
void prt_tbl(int* tbl){
int i,j;
for(i=0;i
printf("%X",tbl[i*side]);
for(j=1;j
printf(",%X",tbl[i*side j]);
putchar('\n');
}
}
int stp_c=1;
#define dbg_c 0xd
main(){
head=malloc(sizeof(trl));
int malloc_c=1;
*head=trl_init;
trl *cur,*insert_end;
for(;stp_c
printf("\n========stp_c:%X=======\n",stp_c);
if(head==NULL){
printf("possiblity exhuasted without finding suitable trail\n");
exit(EXIT_SUCCESS);
}
trl** prev_p;
for(cur=head,prev_p=&head;cur!=NULL;cur=insert_end){
insert_end=cur->next;
stp_gen(cur);
int vs_i;
if(cur==head){
prt_tbl(cur->tbl);/
printf("\nmalloc_c:%d\n---vld_stps:\n",malloc_c);
for(vs_i=0;vs_i<8;vs_i )
printf("[%X]:(%X,%X)\n",vs_i,vld_stps[vs_i].i,vld_stps[vs_i].j,malloc_c);
}
stp temp;
trl *n_trl;
for(vs_i=0;vs_i<8&&(temp=vld_stps[vs_i]).i>-1;vs_i ){
n_trl=malloc(sizeof(trl));
malloc_c ;///
if(n_trl==NULL){
printf("hahahah\n");
exit(EXIT_SUCCESS);
}
memcpy(n_trl->tbl,cur->tbl,sizeof(int)*side*side);
n_trl->cur_stp=temp;
n_trl->tbl[temp.i temp.j*side]=stp_c;
*prev_p=n_trl;
prev_p=&n_trl->next;
}
if(vs_i!=0)
n_trl->next=insert_end;
else
*prev_p=insert_end;
free(cur);
malloc_c--;
}
}
}
#define in_tbl(i,j) ((i)<7&&(i)>-1&&(j)<7&&(j)>-1)
stp gen_tmpl[]={ {1,2},{1,-2},{-1,2},{2,1},{2,-1},{2,{2,1},{2,-1};
void stp_gen(trl * trl){
int i=trl->cur_stp.i,j=trl->cur_stp.j,i_tp,j_tp;
int gen_i;
int vld_stp_i=0;
for(gen_i=0;gen_i<8;gen_i ){
i_tp=i gen_tmpl[gen_i].i;
j_tp=j gen_tmpl[gen_i].j;
if(in_tbl(i_tp,j_tp)&&trl->tbl[i_tp j_tp*side]==0){
stp temp={i_tp,j_tp};
vld_stps[vld_stp_i ]=temp;
}
}
if(vld_stp_i<8)
vld_stps[vld_stp_i].i=-1;//This indicates the end of valid steps!!
}